你们还记得那场比赛,你们有一块木板并试图让球穿过迷宫而不会陷入陷阱吗?我正在克隆它(除了在立方体的六边)。而不是陷阱,我添加了炸弹,玩家必须在进展前积极解除武装。我最近添加了一些代码以使其更加用户友好,但我一直在收到错误:
UnityException:声明变量时不允许调用此函数。没有变量声明后将其移动到行。如果您使用的是C#,请不要在构造函数或字段初始值设定项中使用此函数,而是将初始化移至Awake或Start函数。 UnityEngine.GameObject..ctor()(在C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineGameObject.gen.cs:419)RandoLetter..cctor()Rethrow as TypeInitializationException:抛出异常RandoLetter的类型初始值设定项
RandoLetter是为玩家创建销毁炸弹的代码的脚本,但由于某种原因它起作用。这是代码:
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;
}
}
}
}
对不起,如果它看起来像一堆垃圾,我试着尽可能地评论它。无论如何,它已经困扰了我一段时间,我真的很感激任何帮助:D
答案 0 :(得分:1)
您必须在函数内创建一个新的GameObject实例。
替换public static GameObject Particles = new GameObject();
带
public static GameObject Particles;
void Awake()
{
Particles = new GameObject();
}