Search Unity

Object get's destroyed even though there is no code to destroy it.

Discussion in 'Scripting' started by Chris Aoki, Mar 19, 2013.

  1. Chris Aoki

    Chris Aoki

    Joined:
    Jan 15, 2012
    Posts:
    200
    Hello folks,

    I've run into a problem with this script here.

    Code (csharp):
    1. void Update () {
    2.         if(state == State.Held)
    3.         {
    4.             this.transform.position = new Vector3(player.transform.position.x,player.transform.position.y+2,0);
    5.             this.GetComponent<TrailRenderer>().enabled=false;
    6.             velocity=Vector3.zero;
    7.             if(Input.GetButtonDown("Fire1"))
    8.             {
    9.                 state = State.Moving;
    10.                 velocity=new Vector3(Random.Range(1f,-1f),1f,0);
    11.                 this.GetComponent<TrailRenderer>().enabled=true;
    12.             }
    13.        
    14.         }
    15.         if(state == State.Moving)
    16.         {
    17.            
    18.            
    19.             this.transform.position += velocity * Time.deltaTime * speed;
    20.            
    21.             if(transform.position.y<=-30f)
    22.             {
    23.                
    24.                 state=State.Held;
    25.        
    26.             }
    27.         }
    28.        
    29.        
    30.     }
    If the ball falls off the screen it get's placed just above the player, however if the player moves left or right for a couple of seconds and then launches the ball, the ball get's destroyed. I've checked in the editor and the ball is definitely gone, not far off in some random direction, anyone have any ideas?
     
  2. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Maybe the problem is outside of this Update function?
     
  3. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    If it is being destroyed, and it isn't destroying itself, there is only one conclusion that can be offered. Something else is destroying it. Perhaps you have some code on your player object that makes it destroy the ball. Good luck.
     
  4. Chris Aoki

    Chris Aoki

    Joined:
    Jan 15, 2012
    Posts:
    200
    Nope that can't be it, there is no code in this whole project to destroy this object, on the player there are only 2 lines of code to move it left and right. After placing a Debug.Log I've noticed that the ball disappears right after this line
    Code (csharp):
    1.   if(Input.GetButtonDown("Fire1"))
    , it works at the start of the game but the second time nothing is executed in the if statement, the ball just disappears.
     
    Last edited: Mar 19, 2013
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    There is nothing syntacticly wrong with your posted script, but i would question if the ball ever actually falls below y=-30, as you dont seem to update the velocity variable.

    Anyway, if the GO is being destroyed, then you are destroying it somewhere

    try using debug.log to check things, like what State it is every frame, what it's position is (maybe it's hitting a collider which has a destory), try making timescale smaller and watching it move in editor view
     
  6. Chris Aoki

    Chris Aoki

    Joined:
    Jan 15, 2012
    Posts:
    200
    The velocity is set when the ball is initially launched, however it changes when the ball hit's something using this code
    Code (csharp):
    1. void OnCollisionEnter(Collision info)
    2.     {
    3.         foreach(ContactPoint contact in info.contacts) 
    4.         {
    5.             //v1=2*(v.n)*n-v
    6.             velocity =2*(Vector3.Dot(velocity,Vector3.Normalize(contact.normal)))*Vector3.Normalize(contact.normal) -velocity;
    7.             velocity*=-1;
    8.         }
    9.        
    10.     }
    The only call to the Destroy was in the Brick function which destroys a brick when the ball hits one, I've removed that function to see if it was what was destroying the ball. Still the same result, you can launch the ball once, when it falls below -30f it floats above the player again, but when you hit fire, the ball disappears, I beginning to think this might be a bug in the unity engine.
     
  7. faultymoose

    faultymoose

    Joined:
    Oct 1, 2010
    Posts:
    246
    I swear I had a similar problem.

    I had a rigidbody (kinematic) and I was moving it with transform.position =, and I screwed up my script and moved it very very quickly, a huge distance away. I would immediately afterwards get a null reference error. When I fixed the movement code, it went away.
     
  8. Chris Aoki

    Chris Aoki

    Joined:
    Jan 15, 2012
    Posts:
    200
    Well finally fixed it, had to change where I was changing the velocity before the state change. Thanks everyone!
     
  9. Gibbonator

    Gibbonator

    Joined:
    Jul 27, 2012
    Posts:
    204
    Having the 'autodestruct' flag set on your TrailRenderer component could explain what you're seeing.