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 /
avatar image
-1
Question by muffinEmperor · Feb 15, 2017 at 08:41 PM · tagtagsif statementrecursiontagging

nullReferenceException when comparing tag in a search for a specific parent. help.

below is my function i wrote to find a parent with a specific tag. its resulting in a nullReferenceException.

here are the specifics of the the gameobjects and their tags. I will start with the child. childA's tag is "tagA"; parentA's tag is "Untagged"; this is the defualt value if you do not add a tag. parentB's tag is "tagB";

the parental structure is as follows. parentB>ParentA>childA

so childA's parent is ParentA and ParentA's parent is ParentB

This is how im using the function below. its running in a script attached to childA. GameObject parentB = findElderWithTag(gameObject, "tagB");

It should return the gameObject of ParentB.

 private GameObject findElderWithTag(GameObject aGameObject, string aTag){
     if(aGameObject.transform.parent.tag!="Untagged"){
         if(aGameObject.transform.parent.tag==aTag){
             print(aGameObject.transform.parent.gameObject.name);
             return aGameObject.transform.parent.gameObject;
         }
         else{
             return findElderWithTag(aGameObject.transform.parent.gameObject, aTag);
         }
     }
     else{
         print("this should print only once");
         return findElderWithTag(aGameObject.transform.parent.gameObject, aTag);
     }
 }

The error happens on the second line and on the return at the bottom. the print line at the bottom fires once, as it should.

here is the specific error message:

NullReferenceException: Object reference not set to an instance of an object DoorAutoCloser.findElderWithTag (UnityEngine.GameObject aGameObject, System.String aTag) (at Assets/Standard Assets/UserScripts/DoorAutoCloser.cs:42) DoorAutoCloser.findElderWithTag (UnityEngine.GameObject aGameObject, System.String aTag) (at Assets/Standard Assets/UserScripts/DoorAutoCloser.cs:53)

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 muffinEmperor · Feb 15, 2017 at 07:42 PM 0
Share

this was happening as i was executing this in the Awake() function. i moved the execution to the Start() function and there was no error.

avatar image muffinEmperor muffinEmperor · Feb 15, 2017 at 07:44 PM 0
Share

could someone explain why this was happening. are parents not assigned until after awake()?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Feb 16, 2017 at 09:46 PM

No, really. Maybe you had the script on more than one gameobject and that did not have a parent. A little advice though, since your method is very specific and could be generealised a bit more:

 using UnityEngine;
 
 public static class TransformHelper{
 
     public static GameObject FindElderWithTag(this GameObject gameObject, string tag){
 
         Transform current = gameObject.transform;
 
         while (current.parent != null) {
             if (current.parent.CompareTag (tag))
                 return current.parent.gameObject;
             current = current.transform.parent;
         }
 
         return null;
     }
 }

Now you can do that:

 GameObject parentB = gameObject.findElderWithTag("tagB");
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Match 3 game getting the colour of the match 1 Answer

Raycast isn't working when with tag 2 Answers

Making object switch between two tags every 3 seconds 3 Answers

Delete some unused tags and game objects tag changes unity 5 2 Answers

Are Tags the best solution for this? Duplicating an item that duplicates itself a set amount of times. 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