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

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:

GameObject object = new GameObject();

GameObject childObject = new GameObject();

childObject.transform.SetParent(object.transform);

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.

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.