- Home /
The question is answered, right answer was accepted
How to only instantiate objects once?
So I have a world generation script here and it's using an array to pick random blocks but I currently only have one block set up. But the problem is when it generates it creates all the blocks perfectly fine and all but the thing is that it creates like 3 times the blocks on the same position, say if I have a block [here] then there would be 2 other blocks in there occupying the space as well and that causes lag and I don't want that. How do I only create one copy on the place without creating multiple copies of already existing chunks?
Here's my script
var width_x : int;
var height : int;
var width_z : int;
var player : Transform;
var originBlock : Transform;
var Blocks : GameObject[];
function Start () {
player.transform.position.x = originBlock.transform.position.x;
player.transform.position.y = originBlock.transform.position.y + 5;
player.transform.position.z = originBlock.transform.position.z;
}
function Update () {
for(var x : int = 0; x < width_x; x++){
if(x < width_x){
Instantiate(Blocks[Random.Range(0, Blocks.Length)], Vector3(x * 1, 0, 0), Quaternion.identity);
}
if(Input.GetKeyDown(KeyCode.G)){
Debug.Log("Blocks: " + x);
}
}
}
Answer by Klarax · Jul 28, 2014 at 01:01 PM
just put a bool around it and set it to false after completion
What was the working script? Im not sure what you mean by putting a bool around it. I'm trying to create an object when you press a key, but like 5 or 6 pop up at a time, i only want one. This is it so far, so how does the bool look in this script? any please would be awesome lol.
private void Update()
{
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.Z)) {
Instantiate (picker);
Follow this Question
Related Questions
How do I check the distance between multiples objects in an array from the player? 4 Answers
How to save/load the Y-Coordinates of Instantiated Objects 1 Answer
Instantiating objects at different locations 0 Answers
Instantiating road pieces without any spacings between 2 Answers
How to spawn object but with child as center of position 0 Answers