- Home /
Question by
vahidi · Aug 06, 2016 at 07:50 PM ·
instantiate-game-object
how can I stop instantiate process
I want to instantiate a GameObject 200 times in special position (as a block for player) and stop this process after 200. but I dont know how :). this is my code using UnityEngine; using System.Collections;
public class Intastiating : MonoBehaviour {
public GameObject player;
public GameObject block1;
public GameObject block2;
public GameObject[] blocksNum;
int b1Y = Random.Range(-34,29);
void Update ()
{
Instantiate (block1,new Vector3 (b1Y,block1.transform.position.y + 15.5f,0),Quaternion.identity);
Instantiate (block2, new Vector3 (b1Y + 27.4f, block2.transform.position.y + 15.5f), Quaternion.identity);
blocksNum = GameObject.FindGameObjectsWithTag("Block");
if (blocksNum.Length > 200)
{
}
}
}
Comment
Best Answer
Answer by StupidlySimple · Aug 07, 2016 at 01:00 AM
// Always Comment Code. Comment is for you and your team since they aren't complied when you build your game.
// You don't want to spend hours to go through thousands of line of unedited code to kill a single bug!
public class Intastiating : MonoBehaviour
{
public GameObject player; // Player - not initiated (will cause error)
public GameObject block1; // Block Type 1 - Not initiated (will cause error)
public GameObject block2; // Block Type 2 - Not initiated (will cause error)
public GameObject[] blocksNum = new GameObject[0]; // Number of Blocks
int b1Y = Random.Range(-34, 29); // Random X-Position.
void Update()
{
if (blocksNum.Length < 200)
{
Instantiate(block1, new Vector3(b1Y, block1.transform.position.y + 15.5f, 0), Quaternion.identity); // Unnecessary Complex
Instantiate(block2, new Vector3(b1Y + 27.4f, block2.transform.position.y + 15.5f), Quaternion.identity); // Unnecessary Complex
blocksNum = GameObject.FindGameObjectsWithTag("Block"); // Bad Programming.
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612090256im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Looking to destroy clone of gameobject without destroying original 1 Answer
Instantiate as a child properly 1 Answer
Cloned gamObject have no gravity on release/detach,Cloned gameObject have no gravity 0 Answers
Instantiate GameObject on Enum Selection from Inspector 1 Answer
How can I have an access to the rigidbody component of each instantiated object? 1 Answer