How to add sphere to other gameObject during runtime?

var Collection = new GameObject(); = “Collection”;

            var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            sphere.transform.localScale = new Vector3(1f, 1f, 1f);

I created GameObject and sphere during the runtime but how to add the sphere to the Collection by
c# code??
(It’s like when we drag sphere into the GameObject in inspector)


Good day.

Why are you using this? what is the “final objective”? I think you hace C# knowledge, but not in Unity :smiley:

MAybe your best option is to create a prfab and just instantiate it… But anyway, i try to solve your question:

First, what you mean with “add the sphere to colection” Colletcion, as io see in your code is a GameObject variable… You mean make the sphere child of the Collection gameobject?

sphere.tranform.parent = Collection.transform;

But creating primitives is very “rude” you should create the object in the editor window, give to it a material, textures, effects, positions etc… and the store it as prefab, so you only nned to instantiate that prefab by code…

Bye! :smiley: