Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Repeating an Instantiate cycle

Discussion in 'Scripting' started by SudoSoul, Nov 13, 2009.

  1. SudoSoul

    SudoSoul

    Joined:
    Mar 3, 2009
    Posts:
    12
    I'm new to Javascript, so forgive anything obvious I'm missing.

    Problem: I want to re-Instantiate the 'debri' object once the active debri object has been destroyed. What am I missing to allow my initial IF statement from re-executing?

    Any help would be appreciated!

    Q



    Code (csharp):
    1. var debri: GameObject;
    2. var startSpot : Transform;
    3. var endSpot : Transform;
    4. var shootingSpeed : float = 1.0;
    5.  
    6. private var debriAlive : boolean;
    7. private var currentDebri : GameObject;
    8.  
    9.  
    10. function Start () {
    11.     shootingSpeed = Random.value / 2;
    12.     debriAlive = false;
    13.  
    14.  
    15. function Update () {
    16.     if (debriAlive == false) {
    17.         currentDebri = Instantiate (debri, transform.position, transform.rotation);
    18.         debriAlive = true;
    19.     }
    20.     debriSpeed = Time.time * shootingSpeed;
    21.     currentDebri.transform.position = Vector3.Lerp (startSpot.position, endSpot.position, debriSpeed);
    22.     currentDebri.transform.Rotate (Vector3(15, 3, 22) * Time.deltaTime);
    23.  
    24.     if (currentDebri.transform.position == endSpot.position) {
    25.         Destroy (currentDebri);
    26.         debriAlive = false;
    27.     }
    28. }
     
  2. Timmer

    Timmer

    Joined:
    Jul 28, 2008
    Posts:
    330
    Hello,

    What is happening when running this? I assume it gets created once and then never again?

    The only thing I'd look into is you destruction code: it says if position == endSpot.position but that has a small chance of ever being true. What you probably want is "if position >= endSpot.position"
     
  3. SudoSoul

    SudoSoul

    Joined:
    Mar 3, 2009
    Posts:
    12
    Timmer,

    It runs once, with the 'currentDebri' item traveling and then destroying correctly. It is simply not respawning another instance.

    And I see what you are saying about the meeting the endpoint. While I've not a single problem with that as of yet, your edit makes sense.

    Thanks -
     
  4. Timmer

    Timmer

    Joined:
    Jul 28, 2008
    Posts:
    330
    Hmm... maybe someone else will see what's wrong.

    In the meantime, I just recommend putting some Debug.Log() statements to see if the alive flag is being set to false correctly and if the if(!alive) block is being entered, etc.

    The problem may lie with the execution logic instead of the Instantiate() call.
     
  5. ader

    ader

    Joined:
    Jan 27, 2009
    Posts:
    155
    First off, I assume you know that your start function is not closed in the code above, maybe a copy and paste error?

    secondly, I'd maybe try using DestroyImmediate or use invoke to recreate the debris a second later as opposed to a frame later?

    lastly, as Timmer said, your actual first point of call should be to use:

    Debug.Log("debriAlive = " + debriAlive);

    to check it is not being reset or over-ridden from elsewhere.

    I'd also put:
    Debug.Log(" created new debri ");
    inside your if (after the instantiate) to check you get into there.
     
  6. SudoSoul

    SudoSoul

    Joined:
    Mar 3, 2009
    Posts:
    12
    ader -

    Yeah, missed the bracket on a copy/paste, but it's in the code. I've not jumped in with Debuging yet, so that should help out. I'm thinking it's a logic argument that's failing - just not yet smart enough to note it.

    Thanks all,

    Q