Search Unity

Learning C# in Unity, I keep destroying objects wrong

Discussion in 'Scripting' started by TomBrien, May 4, 2012.

  1. TomBrien

    TomBrien

    Joined:
    Apr 28, 2012
    Posts:
    111
    I'm INSTATIATING this lil plane with an animation:



    The animation just works by moving the UV along.
    When the UV reaches the end:
    Code (csharp):
    1. if (index == totalCells) {
    2.     Destroy(gameObject);
    3. }
    and I think instead of removing the plane from the game world, it's removing it from my library of assets (or something?) because it'll only instatiate it once. What am I doing wrong?

    Boy I'm learning how to make games in 3D for the first time and I am screwin up all over the dang place!
     
  2. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    When you do Destroy(gameObject); you're destroying the object that the script is on. Is that what you want?

    More likely, you wanna find the gameobject which represents the plane and destroy that.
     
  3. TomBrien

    TomBrien

    Joined:
    Apr 28, 2012
    Posts:
    111
    So when I instatiate this plane, I should give it a unique name, and then make sure to Destroy("Name!"); ?
     
  4. grim2594

    grim2594

    Joined:
    Nov 5, 2010
    Posts:
    104
    What exactly are you trying to do?

    Destroy does exactly that, it deletes it from the scene. Make sure that you are referencing whatever it is that you want to destroy. Destroy() will destroy the gameObject and everything included with it.
     
  5. TomBrien

    TomBrien

    Joined:
    Apr 28, 2012
    Posts:
    111
    Ok here's what I'm trying to do: I wanna spawn this plane with an animation on it, and then delete it when the animation's done. So it doesn't just hang there and loop forever. Like you would do with a squib or explosion effect.

    In the original post I'm showing that it does delete, but then also never spawns another one. So that's the problem.
     
  6. grim2594

    grim2594

    Joined:
    Nov 5, 2010
    Posts:
    104

    Are you instantiating another? Show your code.
     
    Last edited: May 5, 2012
  7. zombient

    zombient

    Joined:
    Mar 30, 2012
    Posts:
    12
    Hmm, can you show us the piece of code? It depends on how you're instantiating it, I think
     
  8. TomBrien

    TomBrien

    Joined:
    Apr 28, 2012
    Posts:
    111
    Code (csharp):
    1.  
    2. if(Input.GetButtonDown("Fire1"))
    3. {      
    4.     animation.Play("Slash1");
    5.  
    6.     Slash1 = Instantiate(Slash1, transform.position, transform.rotation) as Transform;
    7.     Slash1.transform.Translate(1,0,0);
    8.     Slash1.transform.Rotate(90,90,0);
    9. }
    10.  
    Took me ages to figure out how to instantiate, I think I accidentally followed a javascript tutorial like 3 times!
     
    Last edited: May 4, 2012
  9. grim2594

    grim2594

    Joined:
    Nov 5, 2010
    Posts:
    104
    Try instantiating a gameObject, because that's probably what your destroying.

    Something like this:

    Code (csharp):
    1. GameObject itemObj = GameObject.Instantiate(objectPrefab) as GameObject;
     
    Last edited: May 4, 2012
  10. ackyth

    ackyth

    Joined:
    Oct 29, 2009
    Posts:
    146
    Slash1 = Instantiate(Slash1, transform.position, transform.rotation) as Transform;
    This is Instantiating a gameobject (Slash1) and overwriting Slash1 as that gameobject(Clone).

    You need to keep a ref to the prefab Slash1 and not overwrite it with your instantiated object.

    Transform TempSlash1 = Instantiate(Slash1, transform.position, transform.rotation) as Transform;

    Then you can delete it by Destroy(TempSlash1); but only if TempSlash1 is saved somewhere or you use it in the same function.

    To delete the Instantiated Object you need to save the reference to it or call Destroy() on a script on that object.

    Edit:
    Noticed this
    Slash1 = Instantiate(Slash1, transform.position, transform.rotation) as Transform;
    Slash1.transform.Translate(1,0,0); //transform is not needed as Slash1 is a Transform.
     
    Last edited: May 4, 2012
  11. TomBrien

    TomBrien

    Joined:
    Apr 28, 2012
    Posts:
    111
    Ok- Both those worked. So that's great. I'm gonna make sure the name of the newly instantiated object isn't the same as the prefab, because I understand what I'm fixing there.
    It still shows in the Hierarchy panel as "Slash1(clone)", so that's confusing- but it works now so OK whatever.

    I thought "transform" was just an object property, I didn't know things could be "a transform". I thought everything I was using was an object.

    A seperate problem is that the animation keeps starting on the wrong frame instead of frame 1...


    I think the code for animating the texture is running on the PREFAB, so the one I'm spawning just start on whatever frame the prefab happens to be on... I'm GUESSING that's what's happening, but I barely know.

    Anyway I'm gonna take a break from this.
     
  12. blurededge

    blurededge

    Joined:
    Mar 14, 2012
    Posts:
    255
    As a thought, you could also just give your instantiated object a name and then tell the destroy command to destroy that specific name. That might solve the problem for you. It would look something like this:


    if(Input.GetButtonDown("Fire1")){

    animation.Play("Slash1");

    GameObject SlashFlash = Instantiate(Slash1, transform.position, transform.rotation) as GameObject;

    SlashFlash.name = newSlash;


    newSlash.transform.Translate(1,0,0);

    newSlash.transform.Rotate(90,90,0);

    Destroy(newSlash);

    }


    I'm no wiz coder so no warranty on the above code, but it might get you moving in the right direction.
     
  13. TomBrien

    TomBrien

    Joined:
    Apr 28, 2012
    Posts:
    111
    Nah I tried that before, there's no way of checking which frame the animation's on FROM the _guy_ prefab.

    So I couldn't destroy it on the last frame.
     
  14. grim2594

    grim2594

    Joined:
    Nov 5, 2010
    Posts:
    104

    Try starting your
    Code (csharp):
    1. animation.Play("Slash1");
    after instantiating it. It may be starting it's animation before it's created, or it's animation frames could be wrong. (Just a guess.)
     
  15. TomBrien

    TomBrien

    Joined:
    Apr 28, 2012
    Posts:
    111
    No I mean the 2D plane, not the 3D guy.

    The 2D plane is animated through the complex UV-manipulation code I put on pastebin and linked to before.
     
  16. ackyth

    ackyth

    Joined:
    Oct 29, 2009
    Posts:
    146
    When you instantiate stuff it creates it as "Name"(clone) in the hierarchy.

    transform is a shortcut in GameObject that returns the component Transform on the gameobject.

    Instantiating any Component automatically generates a GameObject as that is the base that the components use.

    Anything that inherits from Monobehavior is a Component.

    The UV's may be getting changed on the shared material level. Think its something like render.mesh vs render.sharedmesh