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 Darkpyro2 · Oct 26, 2020 at 11:21 AM · transformchildrenfor-loopforeach

Unity detects three children of the current object, but only grabs one

I have the following object:

alt text

In a script attached to the TriggerableDoor1 parent object, I have the following start function:

    void Start()
     {
         Debug.Log("Child Count: " + transform.childCount);
         for (int i = 0; i < transform.childCount; i++) //Get the three children objects
         {
             Transform t = transform.GetChild(i);
             Debug.Log(t.name);
             if (t.name == "ClosedLoc" && _closeloc == null)
             {
                 _closeloc = t.gameObject;
             }
             if (t.name == "OpenLoc" && _openloc == null)
             {
                 _openloc = t.gameObject;
             }
             else if (t.name == "Door" && _door == null)
             {
                 _door = t.gameObject;
             }
 
             _openloc.GetComponent<Renderer>().enabled = false; //Disable the renderers for the guides
             _closeloc.GetComponent<Renderer>().enabled = false;
 
         }
     }

I am attempting to get each of the object's three children, and store them within the script. NOTE: This occurs whether I use a for loop with GetChild or foreach(Transform t in transform)

When I boot up the game, however, it only finds the first object in the list. It can be any of the three objects, but ONLY if they are the first listsed.

childCount returns three, however.

Here's the actual debug output:

alt text

It only seems to ever want to grab the first object, despite the fact that it knows there are three.

What am I doing wrong?

I'm using Unity for Linux.

file.png (5.5 kB)
file2.png (12.4 kB)
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
0
Best Answer

Answer by Lucas_Lima_14 · Oct 26, 2020 at 12:46 PM

I checked your script and it returned all the 3 objects and disabled the openloc and closedloc, the default door is properly rendered.

 public class UnityAnswersTest : MonoBehaviour
 {
     [SerializeField] GameObject _openloc;
     [SerializeField] GameObject _door;
     [SerializeField] GameObject _closeloc;
 
 
     void Start()
     {
         Debug.Log("Child Count: " + transform.childCount);
 
         for (int i = 0; i < transform.childCount; i++) //Get the three children objects
         {
             Transform t = transform.GetChild(i);
             Debug.Log(t.name);
 
             if (t.name == "ClosedLoc" && _closeloc == null)
             {
                 _closeloc = t.gameObject;
             }
             if (t.name == "OpenLoc" && _openloc == null)
             {
                 _openloc = t.gameObject;
             }
             else if (t.name == "Door" && _door == null)
             {
                 _door = t.gameObject;
             }
         }
 
         _closeloc.GetComponent<SpriteRenderer>().enabled = false;
         _openloc.GetComponent<SpriteRenderer>().enabled = false; //Disable the renderers for the guides
     }
 }

Comment
Add comment · Show 3 · 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 Lucas_Lima_14 · Oct 26, 2020 at 01:03 PM 0
Share

I just moved the _closedloc and _openloc disable renderers lines out/after the for loop. It fill the variables first and then disable the renderers.

avatar image Darkpyro2 · Nov 03, 2020 at 01:24 AM 0
Share

Your change does not appear to fix the issue. I wonder if this is a bug with the Linux implementation of Unity.

avatar image Darkpyro2 · Nov 03, 2020 at 01:27 AM 0
Share

Never$$anonymous$$d! It did end up working. I wonder why disabling the renderer disabled the objects.

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

181 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 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 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 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 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 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 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 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 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 avatar image

Related Questions

How to sync child objects of a transform in multiplayer 3 Answers

Apply changes to a random child from sublist 1 Answer

What is the order in which transforms are stored in .unity (scene) file? 1 Answer

Which is a faster find? (foreach) or (for-loop) 2 Answers

Why do we access children through an object's transform? 2 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