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 Ochreous · Jun 27, 2013 at 12:36 AM · c#nullreferenceexceptionfunction

C# Null Reference Exception in Custom Function

Hi everyone, I'm trying to create a function that a finds a gameobject with a tag and then adds the gameobject's children to a transform array. When I run the script the console gives me a null reference exception and it points to line 5. Any reason why? I don't see anything that is null here.

 public Transform[] someTransformArray;
 
     void FindGameObjectsChildrenByTag(string tag, Transform[] transformArray){
     GameObject go = GameObject.FindGameObjectWithTag(tag);
     transformArray = go.GetComponentsInChildren<Transform>();//Null Reference Exception
     }
 
         void Awake(){
 FindGameObjectsChildrenByTag("someTag", someTransformArray);
 }
 
 
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
Best Answer

Answer by iwaldrop · Jun 27, 2013 at 01:02 AM

It's very likely that GameObject go is null. You should always do a null check before using a value that you think could be null. Think to yourself, what happens if GameObject.FindGameObjectWithTag doesn't find anything?

 GameObject go = GameObject.FindGameObjectWithTag(tag);
 if (go != null)
     transformArray = go.GetComponentsInChildren<Transform>();
 else if (Debug.isDebugBuild)
     Debug.LogError(string.Format("No GameObject with tag {0} was found.", tag));
Comment
Add comment · Show 8 · 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 Ochreous · Jun 27, 2013 at 06:20 PM 0
Share

I found out that I didn't have it tagged correctly and now I'm not getting a null reference. But now when I run my script someTransformArray doesn't equal go's children.

     public Transform[] someTransformArray;
     
     void FindGameObjectsChildrenByTag(string tag, Transform[] transformArray){
     GameObject go = GameObject.FindGameObjectWithTag(tag);
     if (go != null)
     transformArray = go.GetComponentsInChildren<Transform>();
     else if (Debug.isDebugBuild)
     Debug.LogError(string.Format("No GameObject with tag {0} was found.", tag));
     } 
             void Awake(){
     FindGameObjectsChildrenByTag("someTag", someTransformArray);// Nothing Happens
     }
     
avatar image iwaldrop · Jun 27, 2013 at 06:28 PM 0
Share

Is there anything in it? It should be an array of all transforms under and including the GameObject that you attach the script to. Print them all out...

 Transform[] ts = GetComponentsInChildren<Transform>();
     foreach (Transform t in ts)
         Debug.Log(t.name, t); 
avatar image iwaldrop · Jun 27, 2013 at 06:33 PM 0
Share

You never assign transformArray to someTransformArray. You should either use the ref keyword on your transformArray property, or don't pass it at all and have your function set someTransformArray directly. You should also properly indent your code.

avatar image iwaldrop · Jun 27, 2013 at 06:43 PM 0
Share

I don't understand what you're saying. Of course someTransformArray doesn't contain anything, because you never assign anything to it (like I said in my previous comment). I don't know if you want it to be set or not now, but I've told you what to do it you do want it set.

avatar image Ochreous · Jun 27, 2013 at 06:48 PM 0
Share

How do you use the ref keyword? I tried putting it next to transformArray and I'm getting an error from the console saying I have invalid arguments

     void FindGameObjectsChildrenByTag(string tag, ref Transform[] transformArray){
 GameObject go = GameObject.FindGameObjectWithTag(tag);
 if (go != null)
 transformArray = go.GetComponentsInChildren();
 else if (Debug.isDebugBuild)
 Debug.LogError(string.Format("No GameObject with tag {0} was found.", tag));
     }
Show more comments

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

16 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

Related Questions

Variables Set by Function Outside of Script 1 Answer

C# Textfield Null Reference Exception 1 Answer

NullReferenceException After A Few Hours? 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Working Reference Still Throwing Null Reference Error 0 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