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

Handling Instances of Prefabs

Discussion in 'Scripting' started by yangmeng, Oct 26, 2007.

  1. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    I would like to know how to keep track of instances of prefabs so that I can delete certain instances at certain times from a game master script.
    So, I would Instantiate "BallObject" a few times and then be able to delete a certain one of them, say the second to be Instantiated, using a script. Is there a way to create variables that keep track of instances of a prefab as they are created? So var Ball1 is the first ball instantiated, Var Ball2 is the second and so on.
    I tried something like this:
    Code (csharp):
    1.  
    2. var Ball1 : GameObject;
    3. Ball1 = Instantiate (ballPrefab, transform.position, transform.rotation);
    4.  
    with
    Code (csharp):
    1.  
    2. Destroy (Ball1);
    3.  
    but couldn't get it to work. Any pointers?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I'm guessing that didn't work because ball1 is a local variable to whatever function it was created in, and you're calling Destroy(ball1) in a different function? Variable names have to be global in order to be accessed from outside a function.

    Also, if you have a number of objects, you'd probably want to use an array of some kind instead of a different variable for each one. If you know beforehand how many objects you want, then use a built-in array:

    Code (csharp):
    1. var ball : GameObject[];
    2. var ballPrefab : GameObject;
    3.  
    4. function Start() {
    5.     ball = new GameObject[10];
    6.     for (i = 0; i < 10; i++) {
    7.         ball[i] = Instantiate(ballPrefab);
    8.     }
    9. }
    10.  
    11. function DestroyBall(whichOne : int) {
    12.     if (ball[whichOne]  whichOne < ball.length) {
    13.         Destroy(ball[whichOne]);
    14.     }
    15.     else {Debug.Log ("Attempted to destroy non-existing ball");}
    16. }
    That would make 10 balls, and you could destroy the second one by doing "DestroyBall(1);" (keeping in mind that the numbering starts at 0).

    You could also use a Javascript array, which provides more flexibility (if, for example, the number of balls varies a lot and you don't want to specify upfront how many there will be). The downside is that this kind of array is slower, but unless you're looping through a lot of balls every frame, it probably won't make a difference.

    --Eric
     
  3. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    Thanks for your response, Eric. Actually, both snips of code are from a function Update... But I will play with the code you provided and see how that works. Once again, the answer is in the arrays!
     
  4. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    What Eric was saying was that if you declare a variable in Update, it gets created and released each time Update runs.

    So to delcare a global variable that is persistent, declare it outside all functions.

    If you want other scripts to be able to see it and for it to appear in the Inspector, it should be public. If not, then it should be private.

    Code (csharp):
    1. public var ball : GameObject;
    2. private var flag : boolean = false;
     
  5. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    Got it now. Thanks!