• 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

Nevermind! 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Weird issue with List and for loop. 1 Answer

Android game lags when using transform.RotateAround to rotate an object with many children 2 Answers

Array foreach problem 1 Answer

How to move a gameObject without affecting its childrens' positions? 2 Answers

Switch for children not working 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges