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 vargata · Jan 13, 2014 at 02:14 PM · nullreferenceexceptionarraysinitialize

array initialisation c#

Hi peepz

I have a silly problem. I have 2 scripts, a master and a guicontrol. the masterscript knows every gameobjects in my scene and have a little function to pass the gameobject array to other scripts:

 using UnityEngine;
 using System.Collections;
 
 public class MasterScript : MonoBehaviour {
         
     private GameObject[] GatesInSpace;
     private GameObject[] FightersInSpace;
 
     // Use this for initialization
     void Start () {
         GatesInSpace = GameObject.FindGameObjectsWithTag("Gate");
         FightersInSpace = GameObject.FindGameObjectsWithTag("Fighter");
     }
     
     // Update is called once per frame
     void Update () {
         FightersInSpace = GameObject.FindGameObjectsWithTag("Fighter");
 
         Debug.Log ("Gates: " + GatesInSpace.Length);
         Debug.Log ("Fighters: " + FightersInSpace.Length);
     }
 
     public GameObject[] GetGates() {
         return GatesInSpace;
     }
 
     public GameObject[] GetFighters() {
         return FightersInSpace;
     }
 }

based on the debug it works well, it find the objects...

the other script:

 using UnityEngine;
 using System.Collections;
 
 public class MapGUI : MonoBehaviour {
 
     public Texture MapGate;
     public Texture MapShip;
     public MasterScript MasterScript;
 
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     void OnGUI () {
         int a = MasterScript.GetGates().Length;
         
     }
 }

sadly throw a nullreference exception :(

why?

Comment
Add comment · Show 2
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 Jamora · Jan 13, 2014 at 02:36 PM 1
Share

Read my explanation of null references. Hopefully it'll help. See this answer.

avatar image vargata · Jan 13, 2014 at 02:54 PM 0
Share

thanx a lot :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by GameVortex · Jan 13, 2014 at 02:33 PM

A guess would be that you have not assigned a value to the MasterScript variable. Try to assign it in the inspector for the GameObject that has the MapGUI component.

Another bugtest you can do is to add debug right before using the master script just to check what is actually null:

 //For example something like this
 Debug.Log("Masterscript is: " + MasterScript);
 Debug.Log("GetGates() is: " + MasterScript.GetGates());


Also as a general coding convention: Class/Types has uppercase names while variables has lowercase names. This avoids confusion and lowers chances of errors.

 //Instead of this
 public MasterScript MasterScript;
 
 //Use this
 public MasterScript masterScript;
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 vargata · Jan 13, 2014 at 02:39 PM 0
Share

ehhh, yes, the script was null, i love the editor... after changing the masterscript variable it removed the attached script but didnt update the inspector. after restarting unity the error was obvious :) thanks for the help

avatar image GameVortex · Jan 13, 2014 at 02:41 PM 0
Share

No problem. Glad to help. =) Also make sure to take a look at @Jamora comment and learn more about how to debug nullreferenceexceptions.

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

20 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

Related Questions

Array assignments not behaving as expected. 2 Answers

[Solved] Actually totally explainable NRE with 2D array 0 Answers

Accessing/Initializing array that's part of a class. 1 Answer

Multidimensional GameObject Arrays 1 Answer

Null reference when accessing GameObject in the Array(C#) 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