- Home /
Generate instances of prefab, then save to GameObject array... How?
I'm trying to make a randomly-generated bridge that explodes when a timer counts down and reaches zero. The bridge is made up of instances of a platform prefab, and is generated with the following code:
public void RoundGen() //Function for generating the level
{
// Set the detonation time to a random integer between 1 and the round number
Time = Mathf.Round(Random.Range(1f, GameController.round) * 10) / 10;
float Target = Mathf.Round(Random.Range(0.5f, 5 * Mathf.Sqrt(GameController.round))*10) / 10; //Set the "correct answer" (Target speed) to a number rounded to the 10ths place
if (GameController.round < 21) //If the level is below 20
{
Target = Mathf.Round(Target); //Round the target to the nearest whole number
}
while (Target * Time != Mathf.Round(Target * Time)) //While the product of target and time variables is not whole
{
Time = Mathf.Round(Random.Range(1f, GameController.round) * 10) / 10;
Target = Mathf.Round(Random.Range(1 / GameController.round, 5 * Mathf.Sqrt(GameController.round)) * 10) / 10;
} // Reassign the two variables until the product is whole
detonationTimer.text = Time.ToString("f"); //Update the detonation timer
Distance = Mathf.RoundToInt(Target * Time); // Set the distance based on the detonation time and target speed
BlockBehavior.Blocks = GameObject.FindGameObjectsWithTag("Platform");
for (int i = 0; i < Distance; i++)
{ // Instantiate the amount of floor blocks needed to build the bridge (Distance) meters long
Instantiate(FloorBlocks, (offset + (Vector3.right * i)), Quaternion.identity);
}
}
Countdown script and coroutine:
void BombCountdown()
{
StartCoroutine(BombCountdownCoroutine());
}
IEnumerator BombCountdownCoroutine()
{
float BombTimer = RoundGenerator.Time;
while (BombTimer > 0 && !PlayerController.fallOver)
{
Display.text = (Mathf.Round(BombTimer * 100) / 100).ToString("f");
BombTimer -= Time.deltaTime;
yield return new WaitForEndOfFrame();
}
ExplodeTrigger = true;
Debug.Log("Countdown finished.");
}
Explosion script and coroutine:
void Update()
{
if (Countdown.ExplodeTrigger) // When the countdown is finished
{
Piece1.GetComponent<Rigidbody>().isKinematic = false; // Remove all constraints
Piece2.GetComponent<Rigidbody>().isKinematic = false;
Piece1.GetComponent<Rigidbody>().useGravity = true;
Piece2.GetComponent<Rigidbody>().useGravity = true;
Piece1.GetComponent<Rigidbody>().AddForce(new Vector3(Random.Range(-15f, 15f), Random.Range(5f, 10f), Random.Range(-15f, 15f)), ForceMode.VelocityChange); // Throw the pieces in random directions
Piece2.GetComponent<Rigidbody>().AddForce(new Vector3(Random.Range(-15f, 15f), Random.Range(5f, 10f), Random.Range(-15f, 15f)), ForceMode.VelocityChange);
StartCoroutine(Explosion()); // Start the explosion coroutine
Countdown.ExplodeTrigger = false;
}
}
IEnumerator Explosion()
{
float TempTimer = 1f;
while (TempTimer > 0)
{
TempTimer -= Time.deltaTime;
yield return new WaitForEndOfFrame();
}
foreach (GameObject blk in Blocks)
{
Destroy(blk);
}
Debug.Log("Blocks destroyed.");
yield return new WaitForEndOfFrame();
}
Piece1 and Piece2 are children objects of the prefab. When I try this code, 1) only the first block is blown up. 2) the clone isn't destroyed. 3) The code only works once i.e. once the next level generates, the code no longer works.
Answer by dkjunior · Oct 15, 2015 at 10:55 PM
In your RoundGen() method, try the following:
BlockBehavior.Blocks = new GameObject[Distance];
for (int i = 0; i < Distance; i++)
{ // Instantiate the amount of floor blocks needed to build the bridge (Distance) meters long
BlockBehavior.Blocks[i] = Instantiate(FloorBlocks, (offset + (Vector3.right * i)), Quaternion.identity) as GameObject;
}
I assume that BlockBehavior.Blocks is defined as GameObject[], and that FloorBlocks is your platform prefab.
Your answer
![](https://koobas.hobune.stream/wayback/20220612041426im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Parenting an instantiated prefab. 1 Answer
Destroying Instantiated Prefab 1 Answer
Prefabs into array than delete them when user interacts 1 Answer
Destroy instances from an array 3 Answers