hi
Can any one say how to instantiate prefabs on a particular prefab ex… i am having 2 prefabs in project.Name them as prefab 1 and prefab 2 now i am instantiating prefab 1 and now i want to instantiate prefab 2 on prefab 1, when prefab 1 is instantiating. Can any one help me with this
2 Answers
2
var Prefab1 = GameObject;
var Prefab2 = GameObject;
function Start () {
instantiater ();
}
function instantiater () {
var x = Instantiate(Prefab1, transform.position, transform.rotation);
var y = Instantiate(Prefab2, transform.position, transform.rotation);
y.transform.parent = x.transform;
}
I assume you’re asking for instantiating a prefab as a child of another prefab. This script is an example of how to do that but you can also just parent the object in Unity and make a prefab from the parent. That prefab will have the child as part of it.
hi thanks for your help i got the solution i am pasting the answer
#pragma strict
var brick : GameObject;
function Start () {
var object = Instantiate(brick, Vector3(transform.position.x,-1.23,transform.position.z),Quaternion.identity);
object.transform.parent = transform;
}
thanks for you reply following error is appearing can you please fix it BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(System.Type, UnityEngine.Vector3, UnityEngine.Quaternion)' was found.
– santosh810666hi i had used following code and attached to the parent game object and now child is creating but the problem is it is not instantiating on the parent object it is instantiating in same position var brick : GameObject; function Start () { var object = Instantiate(brick, Vector3 (0, 0, 10), Quaternion.identity); object.transform.parent = transform; }
– santosh810666Try this: var brick : GameObject; function Start () { var object = Instantiate(brick, transform.position,Quaternion.identity); object.transform.parent = transform; }
– EnglishMuffin123hi its working but i can not able to set position only in y axis with out changing x and z. see the image that i am attaching [18917-test.jpg|18917] in this image i want my cube to be moved in y axis
– santosh810666To set your y axis, use
– EnglishMuffin123transform.position.y. If that's not what your looking for then please show me your full code. Also, please accept my answer since your original question has been answered. [transform.position documentation][1] [1]: http://docs.unity3d.com/Documentation/ScriptReference/Transform-position.html