• 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 /
This question was closed Nov 23, 2017 at 11:13 PM by Bunny83 for the following reason:

Problem is not reproducible or outdated

avatar image
Question by finlay_morrison · Nov 23, 2017 at 05:50 PM · variablenullreferenceexceptioncomponentreferencenull

Component refrence goes null randomly?

I have a script that finds an script called RFormachariumINfo, and stores it as finfo. after i use gameobject.findobjectoftype, it prints the correct statment, as it found a FormachariumInfo, however when i use it in the Purchase() function, i print it's value and it is null and i dont know why.

Here is my script :

 using UnityEngine;
 using UnityEngine.UI;
 
 public class PurchaseManager : MonoBehaviour {
 
     [SerializeField]
     private GameManager gameManager;
     private CurrencyDisplays[] currencyDisplays;
     private FormachariumInfo finfo;
     private OutworldInfo oinfo;
     public Text text;
     private string type;
     private int cost;
     public GameObject purchaseButton;
 
     public void Setup()
     {
         currencyDisplays = GameObject.FindObjectsOfType<CurrencyDisplays>();
         finfo = GameObject.FindObjectOfType<FormachariumInfo>();
         print(finfo);
         oinfo = gameManager.oinfo;
     }
 
     public void SetToolBarType(string _type)
     {
         text.text = "";
         type = _type;
         text.text = text.text + type + "\n\n";
     }
 
     public void SetToolBarCost(int _cost)
     {
         cost = _cost;
         text.text = text.text + cost + "\n\n"; ;
     }
 
     public void SetToolBarDesk(string desk)
     {
         text.text = text.text + desk;
         purchaseButton.SetActive(true);
     }
 
     public void Purchase()
     {
         if (gameManager.coins < cost)
             return;
 
             gameManager.coins -= cost;
             if (type == "Small Formacharium")
             {
                 gameManager.formacharia1++;
                 gameManager.CalculateMaxAnts();
             }
             else if (type == "Medium Formacharium")
             {
                 gameManager.formacharia2++;
                 gameManager.CalculateMaxAnts();
             }
             else if (type == "Large Formacharium")
             {
                 gameManager.formacharia3++;
                 gameManager.CalculateMaxAnts();
             }
             else if (type == "Extra Large Formacharium")
             {
                 gameManager.formacharia4++;
                 gameManager.CalculateMaxAnts();
             }
             else if (type == "Small Outworld")
             {
                 gameManager.outworlds1++;
                 gameManager.FindMaxFood();
             }
             else if (type == "Medium Outworld")
             {
                 gameManager.outworlds2++;
                 gameManager.FindMaxFood();
             }
             else if (type == "Large Outworld")
             {
                 gameManager.outworlds3++;
                 gameManager.FindMaxFood();
             }
             else if(type == "Extra Large Outworld")
             {
                 gameManager.outworlds4++;
                 gameManager.FindMaxFood();
             }
             else if (type == "Mealworm")
             {
                 gameManager.mealworms++;
             }
             else if (type == "Superworm")
             {
                 gameManager.superworms++;
             }
             else if (type == "Cockroach")
             {
                 gameManager.cockroaches++;
             }
             else if (type == "Water")
             {
                 gameManager.water++;
             }
             else if (type == "AntProduction")
             {
                 gameManager.antProductionLevel++;
             }
             else if (type == "MaxAnts")
             {
                 gameManager.antCapacityLevel++;
             }
         print(finfo);
         finfo.SetDisplay();
         oinfo.SetDisplay();
         gameManager.CalculateMaxAnts();
         gameManager.CalculateBonus();
         foreach (CurrencyDisplays display in currencyDisplays)
         {
             display.SetText();
         }
     }
 
 
 
 }
Comment
pako

People who like this

-1 Show 0
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

3 Replies

  • Sort: 
avatar image
Best Answer

Answer by finlay_morrison · Nov 23, 2017 at 09:05 PM

aaaand the biggest facepalm of 2017!!!!!

i had the script attached to two gameobjects....

Comment
ShadyProductions

People who like this

1 Show 0 · Share
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
avatar image

Answer by Bunny83 · Nov 23, 2017 at 06:45 PM

Component references never "randomly goes null" but do become a fake-null reference when the component or the gameobject it's attached to was destroyed. This can happen either by explicitly calling Destroy or DestroyImmediate on the component or gameobject reference or by loading a new level which will automatically destroy all objects from the last level.

Comment

People who like this

0 Show 2 · Share
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
avatar image finlay_morrison · Nov 23, 2017 at 08:26 PM 0
Share

Im not destroying anything

avatar image finlay_morrison · Nov 23, 2017 at 08:33 PM 0
Share

Strangely they are not null in the inspector when i look at it at runtime, just it is getting a null reference exeption

avatar image

Answer by ShadyProductions · Nov 23, 2017 at 08:31 PM

Where do you call Setup?

As a quick solution, you can make it lazy loaded. (but should not be required if you do it the right way). This way it is loaded and cached for later use, but only when you first access it. So not at the start of the level.

Lazy solution:

     private FormachariumInfo _finfo;
     private FormachariumInfo finfo
     {
         get
         {
             if (_finfo == null)
                 _finfo = GameObject.FindObjectOfType<FormachariumInfo>();
             return _finfo;
         }
     }

access it by calling finfo

Comment

People who like this

0 Show 1 · Share
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
avatar image finlay_morrison · Nov 23, 2017 at 08:55 PM 0
Share

the print statement inside the steup it fine, but its all the variables, Apart from gameManager are null inside the function.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

79 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

Related Questions

I don't understand NullReferenceException: Object reference not set to an instance of an object 0 Answers

Null Reference Exeption?? 0 Answers

How to get a variable value before it was null ? 2 Answers

NullReferenceException Help please :> 1 Answer

NullReferenceExeption when I use Mask 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