Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to spawn new puzzle pieces?

Discussion in 'Scripting' started by videoanime, Feb 20, 2015.

  1. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42
    Hello, I want to make a puzzle game, which consists in a pair of pieces appearing in the top of the screen, then start descending and when this pieces touch the bottom or stop moving, it's supposed that new pieces have to appear.

    The closer I've been achieved this is with a code in my OnCollisionEnter2D

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D coll)
    2.         {
    3.         if (coll.gameObject.layer == 8 || DetectaPiezas.cadena)
    4.             {
    5.             semillero = Semillero.instancia ();
    6.             if (Mathf.Floor (Mathf.Abs (elemento.rigidbody2D.velocity.y)) == 0 && elemento.transform.position.y < 7.1)
    7.                 {
    8.                 if (suelo)
    9.                     semillero.Spawn();
    10.                 }
    11.             }
    12.         }
    The problem here is that a piece is created for every piece that accomplished these conditions, I've tried reading variables in the script that manages the movement of the pieces but I haven't had success, I've tried with courutines but I've failed too, any hints or thoughts?

    I want to execute my Spawn function once and just once everytime the conditions before mentioned occur. How could I detect this?

    Thanks.
     
  2. iDontLikeHipsters

    iDontLikeHipsters

    Joined:
    Mar 24, 2014
    Posts:
    24
    I'm having trouble understanding your problem. Are you saying that it's spawning a new piece for each existing piece instead of individuals? Where are you handling this function?
     
  3. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42
    Yes, yes, you're right, for example, if I have one piece, the next spawn is one new piece, then, when the 2nd touches the bottom, 2 new appear, then 4, then would appear 8 and so on, I'm calling the Spawn function on my On Collision Enter 2D as you can see in my code.

    So, I want to call Spawn just one time regardless the pieces there are in the screen, like in a puzzle game.

    I have a script attached to every random piece which manages the movement of the pieces and it has my oncollisionenter2D too, and I have a script in other gameobject that spawns the new pieces from the top of the screen.