简体   繁体   English

Unity中的C#脚本错误

[英]C# Script in Unity error

I'm getting this weird error when trying to compile this simple console game of guessing numbers. 尝试编译这个简单的猜数字控制台游戏时,出现了这个奇怪的错误。 This worked fine in my desktop but currently I'm using my laptop and can't get this WORKING game to compile. 这在我的台式机上运行良好,但目前我在使用笔记本电脑,无法编译此工作游戏。 The error I get is The associated script can not be loaded. Please fix any compile errors and assign a valid script. 我得到的错误是The associated script can not be loaded. Please fix any compile errors and assign a valid script. The associated script can not be loaded. Please fix any compile errors and assign a valid script. I've tried from rewriting the code to testing it with simpler methods and still wont work. 我尝试过从重写代码到使用更简单的方法对其进行测试,但仍然无法正常工作。

Here is the code itself: 这是代码本身:

using UnityEngine;
using System;
//using System.Collections;

public class NumberWizards : MonoBehaviour {

    // Use this for initialization

    int max;
    int min;
    int guess;
    //string userInput = "input";

    void Start () {
        StarGame();

    }

    void StarGame() {
        max = 1000;
        min = 1;

        print ("========================");
        print ("Welcome to Number Wizard");

//      print ("Give a max range of numbers: ");
//      userInput = Console.ReadLine();
//      max = Convert.ToInt32(userInput);
//      
//      
//      print ("Give a min range of numbers: ");
//      userInput = Console.ReadLine();
//      min = Convert.ToInt32(userInput);
//      
//      while(min == max) {
//          Console.WriteLine("The ranges must be different!");
//          print ("Give a min range of numbers: ");
//          userInput = Console.ReadLine();
//          min = Convert.ToInt32(userInput);
//          
//      }
        max = max +1;
        guess = max - min;
        print("Think of a number and don't tell me. ");

        print ("The highest number you can pick is " +  max); 
        print ("The lowest number you can pick is " + min);     
        print ("Is the number higher or lower than "  + guess);
        print ("Up arrow = higher, down = lower, return = equal");

    }

    void NextGuess() {

        guess = (max + min) / 2;
        print ("Higher or lower than " + guess);
        print ("Up arrow = higher, down = lower, return = equal");

    }

    // Update is called once per frame
    void Update () {

        if(Input.GetKeyDown(KeyCode.UpArrow)) {
            //print("Up arrow key was pressed");

            min = guess;
            NextGuess();


        }else if(Input.GetKeyDown(KeyCode.DownArrow)) {
            //print("Down arrow key was pressed");

            max = guess;
            NextGuess();

        } else if(Input.GetKeyDown(KeyCode.Return)) {
            print("I won");
            StarGame();
        }


    }
}

Here's a list of things to check. 这是要检查的事项列表。

  1. Ensure that there is no compile time errors. 确保没有编译时错误。 Building in MonoDevelop, and verify there are no errors on the console in Unity. 在MonoDevelop中进行构建,并验证Unity中的控制台上是否没有错误。
  2. Ensure that the name of the class matches the file name. 确保类名与文件名匹配。 This was my mistake;-) 这是我的错误;-)
  3. Try switching out the script and putting something else in, to see if there was a glitch of sorts. 尝试关闭脚本并放入其他内容,以查看是否存在故障。

该错误意味着您的类调用了NumberWizards.cs以外的名称,但不幸的是,虽然通常可以在C#中根据需要命名文件,但是如果希望UnityEditor看到它们,则必须将MonoBehaviours命名为正确命名的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM