Search Unity

Cannot Instantiate a destroyed object

Discussion in 'Scripting' started by StreetCodeStudios, May 28, 2015.

  1. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    Hi,
    I have a game where you have a tank and right now there a 2 turrets that look at the player and instantiate a Projectile prefab in a random number of seconds. The problem is when i try to destroy the projectile after collision the turrets can no longer shoot. It also gives me the error "The object of type 'Rigidbody' has been destroyed but you are still trying to access it."

    My TurretShoot Script
    Code (csharp):
    1.  
    2.     public Rigidbody clone;
    3.     public float speed;
    4.     public int minTime;
    5.     public int maxTime;
    6.  
    7.  
    8.  
    9.  
    10.  
    11.     // Use this for initialization
    12.     void Start ()
    13.     {
    14.         StartCoroutine("WaitAndShoot");
    15.    
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update ()
    20.     {
    21.  
    22.     }
    23.  
    24.     IEnumerator WaitAndShoot()
    25.     {
    26.         // suspend execution for 5 seconds
    27.         while (true)
    28.         {
    29.             yield return new WaitForSeconds (Random.Range(minTime, maxTime));
    30.             clone = Instantiate (clone, transform.position, transform.rotation) as Rigidbody;
    31.             clone.AddForce (transform.forward * speed);
    32.         }
    33.  
    34.     }
    35. }
    36.  
    My Destroy projectile script

    Code (csharp):
    1.  
    2.     // Use this for initialization
    3.     void Start ()
    4.     {
    5.    
    6.     }
    7.    
    8.     // Update is called once per frame
    9.     void Update ()
    10.     {
    11.    
    12.     }
    13.  
    14.     void OnCollisionEnter(Collision collision) {
    15.         Destroy (gameObject);
    16.     }
    17. }
    18.  
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,445
    Could be that you have assigned your projectile from Scene window?
    Assign the reference from Project window instead.
     
  3. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    I had assigned it from the project window and just retried to make sure and it did the same thing.
     
  4. Talthilas

    Talthilas

    Joined:
    Apr 1, 2014
    Posts:
    44
    In your WaitAndShoot() method:

    Code (CSharp):
    1. clone = Instantiate (clone, transform.position, transform.rotation) as Rigidbody;
    You are instantiating and setting a clone of the clone. Try one of these methods:

    Rigidbody newClone = (Rigidbody)Instantiate (clone, transform.position, transform.rotation);
    newClone.AddForce(transform.forward * speed);

    or just

    (Rigidbody)Instantiate (clone, transform.position, transform.rotation).AddForce(transform.forward * speed)
     
  5. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    I couldnt get either of these to work. The first one gave me the same result and the second one it wasnt showing it was correct in monodevelop. Thank you tho.
     
  6. Talthilas

    Talthilas

    Joined:
    Apr 1, 2014
    Posts:
    44
    Ok sorry, I was trying to take a shortcut without checking it first o_O

    Just try this again. Cut and paste.
    Code (CSharp):
    1.  
    2. Rigidbody projectile = Instantiate(clone, transform.position, transform.rotation) as Rigidbody;
    3. projectile.AddForce(transform.forward * speed);
    4.  
    Otherwise maybe try placing the yield return line in your WaitAndShoot function after the Instantiate and addforce calls. See if that helps.
     
    StreetCodeStudios likes this.
  7. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    Thanks porters! That worked. Ive been trying to figure this out for 2 days. Thanks again man!!
     
  8. Talthilas

    Talthilas

    Joined:
    Apr 1, 2014
    Posts:
    44
    No worries mate. What worked? The code above, or moving the yield return line in your WaitAndShoot function?