x


Why do I instantiate too many prefabs?

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);
}
}
more ▼

asked Apr 29, 2013 at 11:47 AM

Sketchwhale gravatar image

Sketchwhale
1 2 5 6

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
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.

more ▼

answered Apr 29, 2013 at 12:57 PM

MountDoomTeam gravatar image

MountDoomTeam
1.2k 196 190 206

(comments are locked)
10|3000 characters needed characters left

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;
    }
    }
more ▼

answered Apr 29, 2013 at 01:26 PM

delVhar gravatar image

delVhar
31 1 2 3

Thank you delVhar. That did the trick :)

ZoomDomain, I'm not sure I understood your suggestion :)

Now I just need it to stop phasing through the frame of the playing field.

Apr 29, 2013 at 06:31 PM Sketchwhale
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2853

asked: Apr 29, 2013 at 11:47 AM

Seen: 544 times

Last Updated: Apr 29, 2013 at 06:31 PM