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 iHaveReturnd · Nov 24, 2012 at 09:30 PM · variablegetcomponentchildrenforloop

Problem accessing variable for each child

I'm trying to run through each child of a parent object and check if a variable is true.

I have this:

 for(var child : Transform in transform){
     if(child.gameObject.isGoalNode){
         //do stuff
     }
 }

isGoalNode is a variable in the script attached to each child individually. If I print the child.gameObject.isGoalNode, I get null.

I've also tried it through GetComponent:

 for(var child : Transform in transform){
     var childScript = GetComponent(RepairMiniGame);
     print(childScript.isGoalNode);
     if(child.gameObject.isGoalNode){
         //do stuff
     }
 }

Which turns up a NullReferenceException.

I'm sure I'm just missing something in the logic, so I thought someone here might know what I'm doing wrong.

Thank you!

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 T27M · Nov 24, 2012 at 10:06 PM

You are accessing the component but you are not using it.

 var someScript : ExampleScript; // Declare your type
     someScript = GetComponent (ExampleScript); // Get the component
     someScript.DoSomething (); // Access a variable in that component


If you use GetComponent() it will look for the component attached to the GameObject you are calling it from. Which is not what you want.

In your case it would be something like:

     for ( var i = 0; i < number of children here; i++ )
     {
         var childScript = gameObject.transform.GetChild(i).GetComponent(RepairMiniGame);
              if ( childScript.isGoalNode )
              {
                 Debug.Log( childScript.gameObject.name );
                 
                 //Do Something
             }
 
     }

Where i is the index of the current loop and i < 3 if you have 3 child objects. Remember your first child is at index 0.

Edit: While I was whiping this up quickly I failed noticed that the order wasn't determined by the hierarchy. This shouldn't be a problem if you are just looping through them all.

*Edit: Remember isGoalNode has to be a public bool to access it.

Hope this helps.

Comment
Add comment · Show 1 · 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 iHaveReturnd · Nov 24, 2012 at 10:27 PM 0
Share

I was still getting nullreferenceexception when I changed to this, but that's because I had a child to the object that wasn't one with that script on it.

Once I took that out your example worked. Thank you :)

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

11 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

Related Questions

Accessing a variable within a function.... GetComponent() 2 Answers

GetComponent with variable script possible? 1 Answer

How can i change the scriptable object variable in child script (if it's possible) 0 Answers

How to get all children gameobjects 5 Answers

GetComponent from string name? 3 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