Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by A_Random_Guy · Aug 19, 2016 at 10:24 PM · unity 5nullreferenceexception

UnityException: Not allowed to call this function

Hey guys! 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. 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. 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

RandoLetter is the script that creates the code for the player to destroy the bomb, but its acting up for some reason. 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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

101 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity NullReferenceException on Sprite import 0 Answers

NullReferenceException: Object reference not set to an instance of an object, again 2 Answers

NullReference on updating animator parameter 1 Answer

NullRefrenceException - Cant find the cost 1 Answer

Varialbes on prefab script always null 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges