- Home /
Order of instantiated objects
I made a test script that looks like this:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TestScript : MonoBehaviour {
public bool Test = true;
public bool _Test;
public GameObject TestObject;
public Transform _TestObject;
public GameObject[] TestArray;
void Start()
{
}
void Update()
{
if (_Test != Test)
{
_Test = Test;
Debug.Log("Changed");
TestArray = GameObject.FindGameObjectsWithTag("clone");
foreach (GameObject Clone in TestArray)
{
Destroy(Clone);
Debug.Log("Destroyed");
}
for (int i = 0; i < 4; i++)
{
Debug.Log(i);
Instantiate(TestObject, _TestObject);
TestObject.tag = "clone";
TestObject.name = "Clone" + i;
}
}
}
}
As you see I am instantiating an object and giving it a name for ease, but when I look in the inspector it looks like this:
And I want it to look like this:
I've tried setting the sibling index to the i variable but it gives the same result. Can someone explain to me why this is happening and tell me how I can fix it. (Also when I first start the game it doesn't instantiate anything only after I change the variable ones it does.)
try this:
for (int i = 0; i < 4; i++)
{
Debug.Log(i);
GameObject Clone = Instantiate(TestObject, _TestObject) as GameObject;
Clone.tag = "clone";
Clone.name = "Clone" + i;
Clone.transform.SetSiblingIndex(i);
}
Your code is a little bit confused can you give me the specification for everything you want it to do and I can rewrite it with some comments.
Duplicate of link text
Please don't post the same/similar question twice!