C# attach GO to script's parent


I’ve the follwing Setup:

A GO somewhere in my worldspace.
A script attach to that.
In that script a new GO(myNewCube) spawns and i want it to be a parent of the first GO (with script attached).

How can i do this?

myNewCube.transform.parent = transform.parent.gameObject.transform;

Don’t work.

Problem is, it must be dynamic and i can’t search for the scripts name because i would get back alot of them.

If the myNewCube is created in the parent GO (the one with the script). Then simply set the parent to be the transform of the script/component.

 myNewCube.transform.parent = transform;

Ok, i told you something wrong. The script is not attached to a GO but a prefab. And it spawns a prefab. :confused:

Sure, it looks like this:


By now, the script tries to add the new prefab to the upper prefab, which has the script attached. Either i can add the new prefab to the upper GO, or i create a totaly new GO to get all these prefabs spawned by the script. I can write the second solution, but the first solution would be the cleaner one…