Hi, I have this initial block spawning script for a tetris clone.
When a new block instantiates after the very first, several blocks instantiate on top of each other at once.
Can anybody see an explanation in my script?
//Spawn.js
var Tetriminos : GameObject[];
function Start () {
Create();
}
function Create (){
Instantiate(Tetriminos[Random.Range(0,Tetriminos.Length)], transform.position, transform.rotation);
}
//NewBlockScript.js
var FallingRate : float = 0.5;
function Start () {
Falling();
}
function Falling(){
while(true){
this.transform.position.y += -FallingRate;
yield WaitForSeconds(0.2);
}
}
function OnTriggerEnter (other : Collider) {
if (other.gameObject.CompareTag ("Blue")) {
var go = GameObject.Find("Spawner");
go.GetComponent(Spawn).Create();
Destroy(this);
}
}
var go = GameObject.Find(“Spawner”);
go.GetComponent(Spawn).Create();
that line is causing the extra instantiations, do a Debug.Log line so you can see when and how that create line is triggered. i think its colliding many times.
it is possible to make recursive instantiations so each instantiat object creates i.e. 2 more inside itself and so on.
I suspect its entering several triggers at once, firing multiple calls to Create();
I would suggest having a boolean to determine if the block has “landed” checked after each “falling” call, with the OnTriggerEnter function changing the bool to true if it fires.
For example:
var Tetriminos : GameObject[];
var landed : bool;
function Start () {
Create();
}
function Create (){
Instantiate(Tetriminos[Random.Range(0,Tetriminos.Length)], transform.position, transform.rotation);
}
//NewBlockScript.js
var FallingRate : float = 0.5;
function Start () {
Falling();
}
function Falling()
{
while(true){
this.transform.position.y += -FallingRate;
if(landed)
{
var go = GameObject.Find("Spawner");
go.GetComponent(Spawn).Create();
Destroy(this);
}
yield WaitForSeconds(0.2);
}
}
function OnTriggerEnter (other : Collider) {
if (other.gameObject.CompareTag ("Blue")) {
landed = true;
}
}
make sure your clone object itself didn’t have a clone object attached.