Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Bug After instantiating a Game object, unity cant instantiate another game object as a child.

Discussion in 'Editor & General Support' started by PearJuice73, May 30, 2021.

  1. PearJuice73

    PearJuice73

    Joined:
    Jan 11, 2020
    Posts:
    2
    I've run into a bug where if I instantiate a new game object I cant instantiate another game object as a child. For some reason I cant reproduce this in an empty project but I come across it all the time. A possible example:
    Code (CSharp):
    1. GameObject object = new GameObject();
    2.  
    3. GameObject childObject = new GameObject();
    4.  
    5. childObject.transform.SetParent(object.transform);
    6.  
    7.  
    I did manage to find a half solution which is to use a coroutine to wait a millisecond and then create the other object. The problem with this is it causes noticeable flashes.
     
  2. PearJuice73

    PearJuice73

    Joined:
    Jan 11, 2020
    Posts:
    2
    So I found the reason why this is happening. When I was creating the child game object, I was setting the parent with a reference that I was getting with GameObject.findGameObjectWithTag() although I believe this would still happen with any other indirect referencing method. I hope this helps anyone else searching for this rather obscure bug. If you need me to go into more detail or provide code I'm happy to help.