简体   繁体   中英

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. 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.
  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命名为正确命名的文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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