Hello,
i need to have script that at time 0 will create a cube 0. Than every second every cube will duplicate until 6 seconds has passed from start of the game. It should give 64 cubes. It needs to be in C#.
Thats a code that i made. Can someone help me with this/give me a hand or explain my errors?
Thanks in advance.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Spawn : MonoBehaviour
{
public GameObject[] Cubes = new GameObject[64];
public Transform[] CubeLocation = new Transform[64];
public bool[] CubeMovedThisRound = new bool[64];
public bool[] CubeSpawnedThisRound = new bool[64];
public float time = 0.0f;
public int i = 1;
// Use this for initialization
void Start()
{
SpawnFirstCube();
}
// Update is called once per frame
void Update()
{
SpawnCubes();
}
void SpawnFirstCube()
{
Cubes[0] = GameObject.CreatePrimitive(PrimitiveType.Cube);
Cubes[0].transform.position = new Vector3(0.0f, 0.0f, 0.0f);
Cubes[0].transform.parent = this.gameObject.transform;
Cubes[0].name = "Cube 0";
Cubes[0].renderer.material.color = new Color(0.0f, 0.3f, 0.0f);
CubeLocation[0] = Cubes[0].transform;
CubeSpawnedThisRound[0] = true;
}
void SpawnCubes()
{
while (Time.time > time)//runs everyting every 1 sec
{
while (i <= 31) //checks cubes from 0 to 31
{
if (Cubes[i - 1])
if (CubeSpawnedThisRound[i - 1] == false)
for (int j = 0; j < 31 && Cubes[i] == null; j++) //tried without it but it didn;t worked too, this have to ensure that it will add cube to empy array place
{
Cubes[i] = GameObject.CreatePrimitive(PrimitiveType.Cube);
Cubes[i].transform.position = CubeLocation[i - 1].position + new Vector3(1.5f, 0.0f, 0.0f);
Cubes[i].transform.parent = this.gameObject.transform;
Cubes[i].name = "Cube " + i;
Cubes[i].renderer.material.color = new Color(0.0f, 0.3f, 0.0f);
CubeLocation[i] = Cubes[i-1].transform;
CubeSpawnedThisRound[i] = true;
}
print(i);
i++;
}
time++;
for(int j=0; j<63; j++)
{
CubeSpawnedThisRound[j] = false;
}
}
}
}
The first problem I notice is that your variable “i” will never go down. It starts at 1, and then in SpawnCubes() you loop until it gets bigger than 31, but you never reset it.
Secondly, I think the inner loop with “j” is trying to find an empty array slot to store the new cube in, but it’s going to have problems because (1) you want to eventually store 64 cubes, but j never goes higher than 30; (2) you’re iterating with j, but you still use i (not j) when checking whether you’ve found an open slot and for actually creating the new cube; and (3) you create a new cube on every iteration of the loop, rather than only once you’ve found an open slot.
If I were writing this, I’d try to break the functions down into smaller pieces, and I’d rely more on C#'s powerful built-in collection functionality (requires a little extra background knowledge, but you don’t need to worry about indices all the time). It might look something like this:
using UnityEngine;
using System.Collections.Generic;
public class Duplicator : MonoBehavior {
List<GameObject> cubes;
int cyclesCompleted = 0;
public int maxCycles = 6;
public float secondsBetweenCycles = 1.0f;
void Start()
{
GameObject firstCube = // TODO: Create your first cube here, however you want to do it
// Create a collection for holding all of our cubes
cubes = new List<GameObject>();
// Add our first cube to that collection
cubes.Add(firstCube);
}
void Update()
{
while (cyclesCompleted < maxCycles && Time.time/secondsBetweenCycles > 1 + cyclesCompleted)
{
DuplicateAllCubes();
++cyclesCompleted;
}
}
void DuplicateAllCubes()
{
// Create a new collection for holding all the new cubes we're going to mint
List<GameObject> newCubes = new List<GameObject>(cubes.Count); // Reserve space equal to our current collection
// For each cube in our current collection...
foreach (GameObject cube in cubes)
{
// Duplicate that cube, and add it to the new collection
newCubes.Add(DuplicateCube(cube));
}
// Put everything in the new collection into the main collection
cubes.AddRange(newCubes);
}
GameObject DuplicateCube(GameObject cubeToDuplicate)
{
GameObject newCube = (GameObject)Instantiate(cubeToDuplicate);
// TODO: Initialize your new cube however you want
return newCube;
}
}