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?

[169545-screenshot-585.png*_|169545]
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 _= 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_, Peg*.position, Quaternion.identity);
peg.transform.localScale = Peg.scale;_

_peg.name = $“Peg {i + 1}”;
}*_

void SpawnDisks(int i, float n)
{
int currentPeg = i % 3;
//disks positionings data on pegs here
Disk = 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_, Disk*.position, Quaternion.Euler(-90f, 0, 0));
disk.transform.localScale = Disk.scale;_

_disk.name = $“Disk {i + 1}”;
}
}
sorry for the mess I know it’s not the best code you’ve seen

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).

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_, Disk*.position, Quaternion.Euler(-90f, 0, 0));*_

clone.transform.localScale = Disk*.scale;*
clone.name = $“Disk {i + 1}”;