简体   繁体   English

UnityException:不允许调用此函数

[英]UnityException: Not allowed to call this function

Y'all remember that game where you had a wooden board and tried to get the ball through the maze w/o avoiding pitfalls? 你们还记得在那场比赛中您有一块木板并试图使球穿过迷宫而没有避免陷阱吗? I am making a clone of that (except on the six sides of a cube). 我正在对此进行克隆(立方体的六个侧面除外)。 Instead of pitfalls, I added bombs that players have to actively disarm before they can progress. 我没有添加陷阱,而是添加了炸弹,玩家必须先投入炸弹才能使其前进。 I recently added some code to make it more user friendly, but I keep on getting the error: 我最近添加了一些代码,以使其更加用户友好,但我不断收到错误消息:

UnityException: You are not allowed to call this function when declaring a variable. UnityException:声明变量时不允许调用此函数。 Move it to the line after without a variable declaration. 在没有变量声明的情况下将其移至该行。 If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function. 如果使用的是C#,请不要在构造函数或字段初始化程序中使用此函数,而应将初始化移至Awake或Start函数。 UnityEngine.GameObject..ctor () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineGameObject.gen.cs:419) RandoLetter..cctor () Rethrow as TypeInitializationException: An exception was thrown by the type initializer for RandoLetter UnityEngine.GameObject..ctor()(在C:/buildslave/unity/build/artifacts/Generated/common/runtime/UnityEngineGameObject.gen.cs:419)RandoLetter..cctor()重新抛出为TypeInitializationException:由抛出异常RandoLetter的类型初始值设定项

RandoLetter is the script that creates the code for the player to destroy the bomb, but its acting up for some reason. RandoLetter是一个脚本,用于为玩家创建销毁炸弹的代码,但是由于某种原因,它起作用了。 Here is the code: 这是代码:

using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Collections.Generic;

public class RandoLetter : MonoBehaviour { //The array list of bombs public static GameObject Bombs = new GameObject();

 //Array List of Particles
 public static GameObject Particles = new GameObject();

 //The list of colliders on the bombs
 public static Collider Triggers = new Collider();
 //The symbol that shows up
 int LetterID;

 //The speed at which a new symbol appears
 int counter, CounterMax;

 //The number of symbols that appear in one instance
 int LetterCount;

 //Which slot in the array that the player is accessing
 public int entryID;

 //The symbols that appear
 public GameObject[] Arrows = new GameObject[4];

 //The arrow buttons
 public GameObject ArrowBN;
 public static GameObject ArrowBTN;

 //The randomly generated code
 int[] PassCode = new int[4];

 //The player game object to be destroyed
 public GameObject player;

 //The buttons that let the player rotate
 public GameObject MoveUI;

 //The particle effect when the player dies
 public GameObject death;

 //The timer before the player or bomb expires
 int DeathCounter;

 //The code that the player puts in
 int[] YourCode = new int[4];
 int CounterToMax = 0;

 //Is the bomb currently being disarmed?
 public static bool disarming = false;

 void RestoreStart()
 {
     //How much longer until the next symbol appears
     CounterToMax = 0;
     //Is the player attempting to disarm the bomb
     disarming = false;

     //How many symbols show up
     LetterCount = 4;

     //Which slot in the array is the player accessing
     entryID = 0;

     //The UI that lets the player punch in part of the code
     BallMoving.BombU.SetActive(false);

     //Disabling the previous UI for the code
     SetUIFalse();
 }
 // Use this for initialization
 void Start()
 {
     //The timer until the player expires after failing to disarm a bomb
     DeathCounter = 1000;

     //The slot in the array that the player is accessing
     entryID = 0;

     //The amount of characters in the code being presented to the player
     LetterCount = 4;

     //Setting the static value to the public value
     ArrowBTN = ArrowBN;

     //The time between arrows
     counter = 50;

     //Setting CounterMax (the starting value of the counter) to the amount of time represented by counter

     CounterMax = counter;
 }
 // Update is called once per frame
 void Update()
 {
     //If the player is attempting to disarm the bomb
     if (disarming)
     {

         //decrementing counter
         counter--;

         //Disabling the UI that allows the player to move
         MoveUI.active = false;

         //Special code for the last letter in the sequence
         if (CounterToMax == LetterCount && counter == 0)
         {

             Arrows[LetterID].active = false;
         }

         //In the first 3 letters
         if (counter == 5 && CounterToMax != LetterCount)
         {

             Arrows[LetterID].active = false;

         }

         //If the counter goes lower than zero somehow (just a catch in case the code above malfunctions)
         if (counter < 0)
         {

             counter = CounterMax;

         }

         //Randomly Generating the letter
         if (counter == 0 && CounterToMax != LetterCount)
         {

             LetterID = Random.Range(1, 4);
             counter = CounterMax;
             #region AssignLetter
             //Assigning the letter to the value in the array slot
             Arrows[LetterID].active = true;
             PassCode[CounterToMax] = LetterID;
             CounterToMax++;
             #endregion

         }
     }

     //Re-enabling the move UI
     if (!disarming)
     {
         MoveUI.active = true;
     }

     //Enabling the UI that allows the player to punch in the code
     if (CounterToMax == LetterCount && disarming == true)
     {
         ArrowBTN.active = true;
     }
 }

 //Enabling the player to put in a code based off of a UI element
 public void AddDirection(int number)
 {

     YourCode[entryID] = number;
     entryID++;

     //If the player is at the last letter, make sure that the player's code matches the randomly generated one
     if (entryID == LetterCount)
     {

         //Resetting the entryID (slot in the array) for the next time a player defuses a bomb
         entryID = 0;
         CheckCorrect();

     }
 }

 //Checking to make sure that the player entered in the correct code
 public void CheckCorrect()
 {
     for (int i = 0; i < LetterCount; i++)
     {
         //If the player failed......
         if (YourCode[i] != PassCode[i])
         {
             //Destroy the player
             DestroyPlayer();
         }
     }

     //Otherwise destroy the bomb
     DestroyBomb();
 }

 //Re-enabling the player's UI that allows them to move
 public void SetUIFalse()
 {
     disarming = false;
     ArrowBTN.active = false;
 }

 //Destroying the player
 public void DestroyPlayer()
 {
     print("Player Destroyed!");
     SetUIFalse();
     for (int i = 0; i < DeathCounter; i++)
     {
         Destroy(player);
         if (i == DeathCounter - 1)
         {
             //Switching the levels after the player dies
             switch (RotateBlock.level)
             {
                 case 1:
                     Application.LoadLevel("Level1");
                     break;
                 case 2:
                     Application.LoadLevel("Level2");
                     break;
             }
         }
     }
 }

 //Destroying the bomb
 public void DestroyBomb()
 {
     //Resetting everything for the next time the player defuses a bomb
     RestoreStart();
     print("In DestroyBomb");

     for (int i = 0; i < DeathCounter; i++)
     {
         if (i == DeathCounter - 1)
         {
             //Allowing the player to move again
             Rigidbody rb;
             rb = player.GetComponent<Rigidbody>();

             rb.isKinematic = false;
             //Disabling the bomb
             Bombs.SetActive(false);
             //Enabling the particle effect
             Particles.active = true;
             //Destroying the collider on the bomb
             Triggers.enabled = false;
         }
     }
 }

}

Sorry if it looks like a bunch of junk, I tried to comment it as best as I could. 抱歉,如果它看起来像一堆垃圾,我会尽力对其进行评论。 Anyways, it has been bugging me for a while and I really appreciate any help :D 无论如何,它困扰了我一段时间,我真的很感谢任何帮助:D

You have to create a new instance of GameObject inside a function. 您必须在函数内创建GameObject的新实例。

Replace public static GameObject Particles = new GameObject(); 替换public static GameObject Particles = new GameObject();

with

public static GameObject Particles;

void Awake()
{
    Particles = new GameObject();
}

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

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