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

Object not Deleting

Discussion in 'Scripting' started by Seppi, Mar 23, 2013.

  1. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    Code (csharp):
    1. private var wallType : int;
    2. var wall : Transform;
    3.  
    4. function Start () {
    5. wallType = Random.Range(1,4);
    6. }
    7.  
    8. function Update () {
    9. if(wallType == 1){
    10.     Instantiate(wall, transform.position, transform.rotation);
    11.     Destroy(gameObject);
    12. }
    13. }
    It works, but every once in a while the object doesnt delete itself at the end.

    Help??
     
  2. PavolM

    PavolM

    Joined:
    Sep 4, 2012
    Posts:
    37
    Hi :) maybe I dont understand what you want from script to do.
    Well so : (if i get it) :D
    it dont destroy created wall because Destroy(gameObject); destroy object that script is attached to.

    EDIT : Oh and if your wallType is something else then 1, update function dont destroy that object script is attached too because destroy is in IF statement and it has to be 1 to execute.


    you maybe want to check this too :
    Code (csharp):
    1.     // Kills the game object
    2.     Destroy (gameObject);
    3.     // Removes this script instance from the game object
    4.     Destroy (this);
    5.     // Removes the rigidbody from the game object
    6.     Destroy (rigidbody);
    http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html
    hope it helps ;) cheers
     
    Last edited: Mar 23, 2013
  3. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    To clarify, I meant it sometimes doesnt destroy, even if wallType = 1.
     
  4. PavolM

    PavolM

    Joined:
    Sep 4, 2012
    Posts:
    37
    I dont know why i copy your script and add Debug.Log ( wallType );
    and it works great
     
  5. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Try removing the if statement just to ensure it's not the WallType.
     
  6. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    Ok, just so we're all on the same page, I start with two walls that are children of a single empty gameobject. That gameobject has the above script attached. It looks like so:
    $Untitled_zpsecb9b757.png

    If "wallType" = 1, it should delete that gameobject (the two walls included) and create a solid wall, like this:
    $Untitled2_zps2b339040.png

    But sometimes, it does not delete itself, but still creates the wall, resulting in this:
    $Untitled3_zps9f71a909.png

    Hopefully that explains the issue more clearly.
     
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did you check that none of the wall/s you are instantiating have no script which may have a weird side effect? I am actually sure that something else in your project causes this to happen.


    Another question: Why do you instantiate the wall in update and not already in start?

    Code (csharp):
    1. private var wallType : int;
    2. var wall : Transform;
    3.      
    4. function Start () {
    5.     wallType = Random.Range(1,4);
    6.     if(wallType == 1){
    7.         Instantiate(wall, transform.position, transform.rotation);
    8.         Destroy(gameObject);
    9.     }
    10. }
     
  8. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    Thats what i had thought too, but the wall object being instantiated is nothing but a cube with a texture. no scripts at all.
     
  9. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    So if it is not being destroyed, you should be able to reproduce it somehow. You could place a Debug.Log before the Destroy and a Debug.Log after the destruction to check if that is really the problem.
     
  10. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    not sure I understand... why would it possibly create the wall but not destroy itself if both actions are in the same if statement?
     
  11. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    This can certainly not happen. I actually meant it differently and confused even myself :) .
    My actual question is, do you have more code in your actual project and show us only snippets? Is anything else happening in the Update function? Probably yes, otherwise you wouldn't have to place the destruction there.
     
  12. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    As far as this particular script? no.
     
  13. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    I tried putting it in the start function. The same problem still occurs.
     
  14. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I fear you have to share your project or parts of it. It is pretty obvious that other scripts produce that issue. E.g. the Prefab you instantiate has that script in a child object. Or the intersecting walls are produces by another game object that has this script as well... That is way too much guessing for such a simple script.