簡體   English   中英

Unity中的C#腳本錯誤

[英]C# Script in Unity error

嘗試編譯這個簡單的猜數字控制台游戲時,出現了這個奇怪的錯誤。 這在我的台式機上運行良好,但目前我在使用筆記本電腦,無法編譯此工作游戲。 我得到的錯誤是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. 我嘗試過從重寫代碼到使用更簡單的方法對其進行測試,但仍然無法正常工作。

這是代碼本身:

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();
        }


    }
}

這是要檢查的事項列表。

  1. 確保沒有編譯時錯誤。 在MonoDevelop中進行構建,並驗證Unity中的控制台上是否沒有錯誤。
  2. 確保類名與文件名匹配。 這是我的錯誤;-)
  3. 嘗試關閉腳本並放入其他內容,以查看是否存在故障。

該錯誤意味着您的類調用了NumberWizards.cs以外的名稱,但不幸的是,雖然通常可以在C#中根據需要命名文件,但是如果希望UnityEditor看到它們,則必須將MonoBehaviours命名為正確命名的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM