how to instantiate an object from an array above the previously instantiated object from the same array?
Hi everyone...I have searched alot but unable to find a solution so plz help.Basically what i am trying to do is as you can see in the simplified script that i have made an array(it have three items an each is parent of its child objects) and then instantiating these items randomly.what i want is that the first instantiate should occur above an object already in the scene but then the second object should instantiate on above the previous instantiated object and the third one above the second instantiate and so on. Now what i think i want is a way to determine if intantiate(if this is not possible then we can put instantiate in a seperate function and check if that happened or no but i dont understand how to put either of these two checks in if condition) have occured or no,that is the condition for if statement,if its yes then i need a reference to the last instantiation that ocurred so i can put it equal to lastRedgeSet.plz help and also suggest a better way of doing this if any`public class Pcg : MonoBehaviour {
public GameObject[] RedgeSets;
private GameObject LastRedgeSet;
public GameObject FixedRedges;
private Object myist;
void Start () {
LastRedgeSet = FixedRedges;
}
void Update () {
// if(instantiate happened or no Condition here){
//LatRedgeSet = (refrence to last instantiated object required here) }
Vector3 LastRedgeSetPos = LastRedgeSet.transform.position;
Vector3 addheight = new Vector3 (0, 5, 0);
if (Input.GetKeyDown (KeyCode.Z)) {
Instantiate (RedgeSets [UnityEngine.Random.Range (0, 3)], LastRedgeSetPos + addheight, Quaternion.identity);
}
}`
Answer by JedBeryll · Aug 21, 2016 at 06:09 PM
Just have a Transform variable that by default points to the "object already in the scene". You assign this transform as parent of a newly created instance and change the Transform reference to this new object. Next time when you create an instance the previous object will be parent. So all you have to do is always set the saved transform as parent.
thanks for your suggestion but i am not able to clearly understand what you are trying to say if you could just explain a little more
public GameObject[] RedgeSets;
private GameObject LastRedgeSet;
public GameObject FixedRedges;
private Object myist;
private Transform lastObject;
//make this variable public and set it to the default object in the inspector
//or assign it in Start() or however you want
void Start () {
LastRedgeSet = FixedRedges;
}
void Update () {
Vector3 LastRedgeSetPos = LastRedgeSet.transform.position;
Vector3 addheight = new Vector3 (0, 5, 0);
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Z)) {
GameObject newObject = Instantiate (RedgeSets [UnityEngine.Random.Range (0, 3)], LastRedgeSetPos + addheight, Quaternion.identity) as GameObject;
Transform t = newObject.transform;
t.parent = lastObject;
lastObject = t;
//this way the newly created object will be a child of the default object in the map
//after the first new object has been created, every other object will be a child of the previous object
}
}
thanks alot man u r great...this seems to work perfectly
Your answer
Follow this Question
Related Questions
Instantiate a Prefab along the surface of a sphere. C# 0 Answers
How can I in-script create gameobject from prefab? 2 Answers
Integer arrays not comparing properly. 3 Answers
Problems with if-statements (C#) 1 Answer
Error with Instantiate after build 0 Answers