• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by RoloJo · Aug 04, 2013 at 08:42 AM · nullreferenceexception

NullReference Exception with GameObjects across classes

Hi everyone. I have been struggling with this problem and I simply dont understand how or why I am going wrong. I am trying to use StaticBatchingUtility.Combine. To do that I need to create an array of objects. I have a class which has stored in it a public static List of all the objects. When I refer to the List from my main class a NullReference Exception is thrown.

The problem line in my main class where the exception is thrown is the following:

 Oak0Container[Count0]=ObjectLibrary.WorldOakObjects[i];

The ObjectLibrary is as follows:

 public class ObjectLibrary : MonoBehaviour
 {
     public static List<GameObject> WorldOakObjects = new List<GameObject>();
 
 public static void WorldGameObjectsAdd(GameObject AddedGameObject)
     {
         switch (AddedGameObject.tag)//add
         {
             case "Oak":
                 WorldOakObjects.Add(AddedGameObject);
                 break;
 }
 }
 }

Finally the GameObjects are instantiated in the following class:

 public class InGameSpecies : MonoBehaviour
 {
  public GameObject SpeciesInstantiate(string SpeciesName, int IDNameNumber)
     {
 
 var NewGameObject = GameObject.Instantiate(Resources.Load("Oak0"), Position, randomRotation);
                         NewGameObject.name = (SpeciesName + IDNameNumber.ToString());
                         return (GameObject)NewGameObject;
 }}

I hope I have put in sufficient code. The code goes off fine and works. But when I tried to refer to GameObjects that are in other classes it simply doesnt work. I have tried referring to the GameObject returned in the third class with the same result. I can also use print(ObjectLibrary.WorldOakObjects[i].name) in the main class and that correctly returns the name of the object. Can anyone tell me what I am doing wrong?

Thanks in advance.

Comment
Add comment · Show 4
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 pako · Aug 04, 2013 at 09:14 AM 0
Share

The code you have provided doesn't seem to have any problem. So, the problem seems to be elsewhere in code that you don't provide. e.g. you say "I tried to refer to GameObjects that are in other classes it simply doesnt work" but you don't give any details on how you try to refer to these GameObject. $$anonymous$$aybe if you provide some more details it will be easier to get help.

avatar image RoloJo · Aug 04, 2013 at 09:24 AM 0
Share

Hi, thanks for the reply!

It is hard to put all the code up as it is a fair bit; Im trying to show what I feel is the important part so save you reading so much. Here is more code from the main class:

         for (int j=2;j<3;j++)//change 13
         {
             for (int i=0;i<SpeciesInit[j];i++)
             {
                 ObjectLibrary.WorldGameObjectsAdd(GameSpecies.SpeciesInstantiate(SpeciesTag[j],ObjectLibrary.WorldSpeciesCount[j]));
                 print (ObjectLibrary.WorldOakObjects[i].name);
                 switch(j)
                 {
                     case(2):
                         switch(InGameSpecies.RandomNumber)
                         {
                             case 0:
                                 Oak0Container[Count0]=ObjectLibrary.WorldOakObjects[i];
                                                             Count0++;
                                 break;
                     }
                 }
             }
         }

The "print" line works fine, but assigning the same GameObject to OakContainer[] doesnt work; it throws a NullReference exception.

avatar image Jamora · Aug 04, 2013 at 09:27 AM 1
Share

The code you provided doesn't show if you've actually initialized Oak0Container. Have you?

avatar image RoloJo · Aug 04, 2013 at 09:35 AM 0
Share

Ok, awesome. That was the problem! Just did so and its working. Cheers for the help!

0 Replies

· Add your reply
  • Sort: 

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

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

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

Why am I getting this NullReferenceException error when everything seems to be in place ? 1 Answer

NullReferenceException: Object reference not set to an instance of an object 2 Answers

Why does this return a NullReferenceException? 0 Answers

NullReferenceException-Error when trying to GetComponent 3 Answers

[C#] Can't get variable from other script. Yields NullReferenceException 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