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 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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

Null Reference Exception only in second method 1 Answer

Deleting random objects in started project 0 Answers

Trying to make an object appear in a position, but only if that position isn't occupied. 0 Answers

Image UI not enabling C# SOLVED 1 Answer


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