- Home /
[SOLVED] How to make to clone an object and make it parent of the transform?
Hi,
for (int i = minClones; i < maxClones; i++) {
Instantiate(master, RandomPosition(), Quaternion.identity);
}
master is a prefab. this script is attached to a empty;
how can i make this instantiated object, a child of the empty????
ps: ive whatched other similar posts, but none of them seams to help me.
thanks
Exact Duplicate Question: http://answers.unity3d.com/questions/5713/parenting-instantiated-objects
Exact? not really. the result to achive probably is the same, but the way to do it is diferente, because that code doenst work for me. has i said in the ps ... ive watched other similar post's and none of them work for me.
InvalidCastException: Cannot cast from source type to destination type.
[SOLVED]
found the problem. I was making the instanciation of the prefab as a transform, and it should been a GameOBject;
public GameObject master;
and i was doing:
public Transform master;
Answer by qJake · May 14, 2010 at 09:31 AM
Just set the transform's parent property to the game object, it's really easy.
for (int i = minClones; i < maxClones; i++)
{
GameObject go = Instantiate(master, RandomPosition(), Quaternion.identity) as GameObject;
go.transform.parent = transform;
}
im getting...
NullReferenceException: Object reference not set to an instance of an object
yup even im getting the same exception. Narven, did you get a solution for this ?
I have the same problem, and the error seems to be triggered by `go`. $$anonymous$$aybe the Instantiate function didn't return an instance?
hey tudor, is 'go' a GameObject or Transform. make sure it is a GameObject. That went wrong in my case
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
How can I make a game object follow an instantiated game object? 1 Answer
Instantiated GameObject collision without script repetition? 1 Answer
How to change position of child component without affecting the position of the parent 1 Answer
Instantiate a game object in array then change parent 1 Answer