Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by WarPanda · Aug 10, 2013 at 10:06 PM · nullreferenceexceptionsendmessage

NullReferenceException when sending message to another object...really stumped.

Ok, I'm really stumped on this one because this works for some of my objects, but not the PlayerObject. I have it set up that when an object initializes, it sends a message to the TurnController object to add itself to a list to be tracked. This works fine for both my planet objects and my ship objects, but when I attempted to do the same for my player objects, I keep getting a null reference exception saying that the object reference is not set to an instance of an object. This is confusing the heck out of me because I'm essentially re-using the working code from my planet and ship scripts for sending the message to add itself to the list stored by the turn controller.

Here's my turn controller script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TurnControllerScript : MonoBehaviour 
 {    
     List<GameObject> allPlanets;
     List<GameObject> allShips;
     List<GameObject> allPlayers;
     
     public int currentTurnMST = 1;
     //public GameObject currentPlayer = null;
 
     // Use this for initialization
     void Start () 
     {
         allPlanets = new List<GameObject>();
         allShips = new List<GameObject>();
         allPlayers = new List<GameObject>();
     }
     
     // Update is called once per frame
     void Update () 
     {
     
     }
     
     //Execute all actions when Next Turn button is pressed
     void NextTurn()
     {
         currentTurnMST = currentTurnMST + 1;
         Debug.Log ("NextTurn has been called");
         
         foreach(GameObject obj in allPlanets)
         {
             obj.SendMessage ("OnTurnEnd", SendMessageOptions.DontRequireReceiver);
         }
         
         foreach(GameObject obj in allShips)
         {
             obj.SendMessage ("OnTurnEnd", SendMessageOptions.DontRequireReceiver);
         }
         
     }
     
     void AddToList(GameObject sentObject)
     {
         if(sentObject.tag == "Planet")
         {
             allPlanets.Add (sentObject);
         }
         if(sentObject.tag == "Ship")
         {
             allShips.Add (sentObject);
         }
         if(sentObject.tag == "PlayerObject")
         {
             allPlayers.Add (sentObject);
             Debug.Log ("Added " + sentObject + " to list.");
         }
     }
     
     void RemoveFromList(GameObject sentObject)
     {
         if(sentObject.tag == "Planet")
         {
             allPlanets.Remove (sentObject);
         }
         if(sentObject.tag == "Ship")
         {
             allShips.Remove (sentObject);
         }
         if(sentObject.tag == "PlayerObject")
         {
             allPlayers.Remove (sentObject);
         }
     }
 }

And this is the relevant script from both my playerScript (that sends the message from the player object) and my shipScript (does likewise for ship objects when they're created).

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : MonoBehaviour 
 {    
     GameObject turnControl;
     TurnControllerScript turnControllerScript;
     GameObject guiControl;
     GUIControllerScript guiControllerScript;
     
     public int playerNumber = 1;
     public string playerName = "Test Name";
     
     // Use this for initialization
     void Start () 
     {
         //Initialize both the turn controller and gui controller connections
         turnControl = GameObject.Find ("TurnController");
         turnControllerScript = GameObject.Find ("TurnController").GetComponent<TurnControllerScript>();
         
         guiControl = GameObject.Find ("GUIController");
         guiControllerScript = GameObject.Find ("GUIController").GetComponent<GUIControllerScript>();
         
         turnControl.SendMessage ("AddToList", gameObject);
     }
     
     // Update is called once per frame
     void Update () 
     {
         
     }
 }
 
 using UnityEngine;
 using System.Collections;
 
 public class ShipTestScript : MonoBehaviour 
 {
     GUIControllerScript guiControl;
     TurnControllerScript turnControl;
     
     ...
     
     // Use this for initialization
     void Start () 
     {
         guiControl = GameObject.Find ("GUIController").GetComponent<GUIControllerScript>();
         turnControl = GameObject.Find ("TurnController").GetComponent<TurnControllerScript>();
         originalMaterial = gameObject.renderer.material;
         originPOS = guiControl.selectedObject.transform.position;
         
         targetObject = null;
         targetPOS = transform.position;
         
         
         turnControl.SendMessage ("AddToList", gameObject);
     }
     
 

The ships are created after the scene loads (during runtime), while the planets and players are set in the scene before hand. The planets and ships both add to their respective lists fine, but I keep getting an exception error with the player objects at the line in the turn controller script that receives the message from the player object. I tried putting the sendMessage call on the player script into the Update instead of Start method and it worked fine, but it's in the Start method in both ship and planet, so I don't understand the disconnect.

Hopefully this makes sense. Help would be immensely appreciated.

Thanks in advance.

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
0
Best Answer

Answer by whydoidoit · Aug 10, 2013 at 10:17 PM

The ordering of Start functions in your case is such that the AddToList called by the Start function on one of the classes is called before the Start function on the TurnControllerScript so the lists are null.

Either:

  • Move the list initialization to an Awake function (I'd do this)

  • Set the script execution order so that TurnControllerScript executes first

  • Initialize the lists using List< GameObject> someList = new List< GameObject>(); in where you define them (I might do this)

Comment
Add comment · 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 WarPanda · Aug 10, 2013 at 10:34 PM 0
Share

Awesome, thank you. I used your first suggestion (move to an Awake function) and it worked beautifully.

Oddly enough, right before I saw your answer, I noticed I wasn't getting any more NullReference errors, even though I hadn't changed anything in those scripts. (I had moved onto working on something else for a bit). But it still wasn't getting added to the list. I just wasn't getting the reference error. But the Awake suggestion solved it anyways.

Ugh. Computers.

avatar image whydoidoit · Aug 11, 2013 at 07:54 AM 0
Share

Yeah, script execution order causes some hard to find bugs that can sometimes appear as if by magic :S

Could you tick my answer?

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

15 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

Related Questions

Remmember the spawner 2 Answers

Functions work in the Editor, don't work when Built. (Javascript) 0 Answers

Null Reference Exception error on GameObject.SendMessage 1 Answer

SendMessage weird runtime error 2 Answers

TextAsset Giving me NullReference Exception 2 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