2016-08-20 10 views
0

あなたは木製のボードを持っていて、落とし穴を避けて迷路にボールを入れようとしたゲームを覚えていますか?私はそのクローンを作っています(立方体の6面を除いて)。落とし穴の代わりに、私は爆弾を追加しました。私は最近、ユーザーフレンドリーなコードをいくつか追加しましたが、エラーが発生し続けます。UnityException:この関数を呼び出すことはできません

UnityException:変数を宣言するときにこの関数を呼び出すことはできません。変数宣言をしないと、行に移動します。 C#を使用している場合は、コンストラクタまたはフィールドイニシャライザでこの関数を使用しないで、代わりに初期化をAwake関数またはStart関数に移動します。 ():TypeInitializationExceptionとしてRethrow:RandoLetter..cctor()が例外をスローしました。 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

答えて

1

関数内にGameObjectの新しいインスタンスを作成する必要があります。

public static GameObject Particles; 

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

を交換してください

関連する問題