- Home /
Unity Instantiates GameObjects in a wrong order
Trying my best to make this short. I have been working with Unity for about 2 days now and I'm an amateur. I have to make an Extended Tower of Hanoi game. For that I want to first ask the user for the number of Disks they want the puzzle to be solved with, then according to that number diskCount
I want to instantiate the objects (prefabs) in certain positions relative to diskCount
. I'm instantiating the objects using for
loops but it ends up making the last object of the loop the first one and it happens on other loops of instantiation too. Am I missing a point here or is it like a bug with Unity?
Here's the code:
public class Spawn : MonoBehaviour
{
public static int diskCount = 6;
public GameObject[] disk = new GameObject[diskCount];
public Disk[] Disk = new Disk[diskCount];
public GameObject[] peg = new GameObject[3];
public Peg[] Peg = new Peg[3];
public GameObject Base;
void Start()
{
float n = (float)(3 + (diskCount / 3) - 1);
BaseSpawn(n);
for (int i = 0; i < 3; i++)
SpawnPegs(i, n);
for (int i = 0; i < diskCount; i++)
SpawnDisks(i, n);
}
void BaseSpawn(float n)
{
Instantiate(Base, new Vector3(0, 0, 0), Quaternion.identity);
Base.transform.localScale = new Vector3(n - 1, 0.5f, n * 3);
Base.name = "Base";
}
void SpawnPegs(int i, float n)
{
Peg[i] = new Peg(new Vector3(0, n / 3.0f, (i - 1) * (Base.transform.localScale.z / 2.7272727273f)), new Vector3(0.3f, (n / 3.0f) - 0.25f, 0.3f), diskCount / 3);
Instantiate(peg[i], Peg[i].position, Quaternion.identity);
peg[i].transform.localScale = Peg[i].scale;
peg[i].name = $"Peg {i + 1}";
}
void SpawnDisks(int i, float n)
{
int currentPeg = i % 3;
//disks positionings data on pegs here
Disk[i] = new Disk(new Vector3(Peg[currentPeg].position.x, Peg[currentPeg].level, Peg[currentPeg].position.z), new Vector3(40f + (i * 5f), 40f + (i * 5f), 4), currentPeg);
Peg[currentPeg].level += 0.08f;
//disks GameObject size and appearance and spawning here
Instantiate(disk[i], Disk[i].position, Quaternion.Euler(-90f, 0, 0));
disk[i].transform.localScale = Disk[i].scale;
disk[i].name = $"Disk {i + 1}";
}
}
sorry for the mess I know it's not the best code you've seen
Can't find the error, but what happens if you create them one after another, e.g. by waiting for a "space" press each time? Are they still listed in the wrong order in the hierarchy?
I tried putting a timer before calling the spawn methods in each iteration, still each time the first Object should be the last object.
Answer by Bunny83 · Oct 21, 2020 at 07:15 PM
Like rh_galaxy said you essentially used Instantiate wrong. Instantiate takes a source reference of the object that should be cloned and returns the cloned object. It doesn't do anything to the source object. You manipulate the source prefab after you instantiate your object so the next time you instantiate an object you will clone your modified prefab and then modify the prefab again.
That means since you modify the actual prefab in your project, those changes will persist. So when you re-run your game the first element you instantiate will have the scale you set to the last object. As a result all your objects will essentially be rotated one place to the left.
When you instantiate an object you should use the reference that is returned by the Instantiate method and don't touch the actual prefab you used as source. Something like this:
var clone = Instantiate(disk[i], Disk[i].position, Quaternion.Euler(-90f, 0, 0));
clone.transform.localScale = Disk[i].scale;
clone.name = $"Disk {i + 1}";
Funny I made a very simple towers of hanoi some time ago. You can actually play it here, though it was just a proof of concept and very straight forward implementation in 215 lines of code ^^.
Yes yes thank you very much for your reply. I realised my mistake and I took the approach you suggested and it is working beautifully now! Thank you so much again!
Answer by rh_galaxy · Oct 21, 2020 at 04:06 PM
You set the disk[] array prior to calling Instantiate in the Inspector? The same for peg[]. That means you have to drag existing GameObjects to each array (also making it impossible to have diskCount set by the user). Seams you don't use Instantiate as effective as it can be if I'm not lost here. That is if they are all the same model, only scale makes them different?
I would have one Peg object (with a Peg script attached) and one Disk object (with a Disk script attached) as a start (as publics set in the Inspector). Then instantiate THAT object for each disk (and peg). Then set scale and name after like you do now, and set other properties like color or anything else by calling the Disk script for the new object (return value of Instantiate()).
Seams you have too many copies of too many objects now.
To answer your question about wrong order, it is because the object you give name to is the original object that you make a copy of. But you set the name after you have already made a copy of it. If it is like I suspect that you have dragged one Disk object 6 times to the array, that means you will rename it and the next time you make a copy it will be called that ("Disk 2(Clone)" instead of "Disk 3" like you intended).
Thank you so much for the answer. Yes I corrected the first part of your answer and declared only one Disk and Peg as GameObject
and instantiated them in a for
loop. And about the diskCount being declared by me yes I am aware of that and I am tetsting it as an example. I understood the second part as well. Than you very much!
You can get rid of some variables and avoid having two arrays each for keeping track of the disks and pegs. At least this is how I have done in my projects.
public class Spawn : $$anonymous$$onoBehaviour
{
public Disk diskObjOriginal; //drag your disk prefab here in the Inspector
private Disk[] disks = new Disk[diskCount]; //the disk clones
...
void SpawnDisks(int i, float n)
{
Disk diskNew = Instantiate(diskObjOriginal);
diskNew.Init(i, position, scale, color, anything_you_want);
disks[i] = diskNew;
}
}
public class Disk : $$anonymous$$onoBehaviour
{
public void Init(int diskNr, Vector3 position, Vector3 scale, )
{
//properties
name = "Disk" + diskNr.ToString();
transform.position = position;
transform.localScale = scale;
//example for setting the Disk material
$$anonymous$$aterial material = Resources.Load("Disk$$anonymous$$at1", typeof($$anonymous$$aterial)) as $$anonymous$$aterial;
GetComponent<$$anonymous$$eshRenderer>().material = material;
gameObject.SetActive(true);
}
}