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