• 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 DaveK032 · Dec 05, 2016 at 02:19 PM · scripting problemuinullreferenceexceptionobject reference

ObjectReference Not set to an instance of an object

I'm trying to build a 'monthly event' system for my management game and it keep on throwing up the Object reference not set to an instance of an object issue on line 19, but only when the event occurs. The events are called from another script, and yes, I know it's a hideous mess but I haven't worked out how to optimise it yet. So, any help or guidance would be very much appreciated?

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 
 public class EventManager : MonoBehaviour {
     GameObject PopupWindow;
     Text PopupText;
     int RndEvent;
     int populationlvl;
     int nextintake;
 
     public static void EventOccurer(){    
         GameObject PopupWindow = GameObject.FindGameObjectWithTag("PopupWindow");
         int RndEvent;
         int populationlvl;
         int nextintake;
         Text PopupText = GameObject.Find("EventMessageText").GetComponent<UnityEngine.UI.Text> ();
 
         PopupWindow.SetActive (true);
         RndEvent = UnityEngine.Random.Range (1, 13);
         switch (RndEvent) 
         {
         case 1:
             PopupText.text = "Launch Failure! Loss of next intake!";
             populationlvl = WorldManager.PopulationLvl;
             nextintake = WorldManager.PopIntake;
             populationlvl -= nextintake;
             WorldManager.PopulationLvl = populationlvl;
             break;
         case 2:
             PopupText.text = "Landing Failure! Loss of next intake!";
             populationlvl = WorldManager.PopulationLvl;
             nextintake = WorldManager.PopIntake;
             populationlvl -= nextintake;
             WorldManager.PopulationLvl = populationlvl;
             break;
         case 3:
             break;
           //.....A whole lot of currently empty case statements...
         case 12:
             break;
         }
     }
 }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Landern · Dec 05, 2016 at 02:26 PM

Do yourself a favor and break the chaining down a little and output some debugging messages, either the gameobject is not found or the component on the gameobject is not found, either way your assumptions either of the game of the gameobject(it may have a 1 next to the name if instantiating) or the component location is the hierarchy is incorrect and you need to adjust. I would replace line 19 with something like, you also have an issue that you have a field named the same as a local variable called PopupText, choose if you're going to hold this reference or not, pick one. I've changed the names below, but the debugging is what you need to isolate your issue.

 GameObject emt = GameObject.Find("EventMessageText"); // get the GO named EventMessageText
 Text emtText = null;
 
 if (emt == null) // check for null or that there is an instance found of this object
 {
   Debug.Log("EventMessageText gameobject was not found by the name EventMessageText"); // write out to console that the EventMessageText GameObject reference wasn't found, naming issue?
 }
 else // if emt is not null then lets try and get the Text component from it
 {
   emtText = emt.GetComponent<Text>();
 
   if (emtText == null)
     Debug.Log("Could not find Text Component of EventMessageText, emtText is null");
 }

Comment
Add comment · 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 DaveK032 · Dec 08, 2016 at 04:25 PM 0
Share

Okay, I've implemented the logging functions, I've also renamed them as you suggested(along with the gameobjects & their tags in the editor itself) and I've identified the problem. It doesn't appear to be finding the Event message box(which is the parent gameobject of the EventmessageText) and as such can't find the text component of it

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Null Reference Exception only in second method 1 Answer

How do I set value ScriptableObject list element property in script? 1 Answer

Move UI panel on and off screen, scripted without animation 0 Answers

Using downloaded image as UI Image's source image? 1 Answer

Unity 5.3 UI Button wont set Select state via script after the parent has been set inactive 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges