Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Shooting problem: "The object of type 'GameObject has been destroyed"

Discussion in '2D' started by JackReivaj, Feb 13, 2021.

  1. JackReivaj

    JackReivaj

    Joined:
    Feb 7, 2021
    Posts:
    33
    I have this code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Bullet : MonoBehaviour
    6. {
    7.     public float speed;
    8.     private Rigidbody2D m_rig;
    9.     public Transform player_pos;
    10.  
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         m_rig = GetComponent<Rigidbody2D>();
    16.         m_rig.AddForce(Vector2.right*speed, ForceMode2D.Impulse);
    17.         Invoke("Destruir_", 2);
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         //Flip bullet
    24.  
    25.         if (player_pos.position.x > this.transform.position.x)
    26.         {
    27.             speed *= -1;
    28.         }
    29.         else
    30.         {
    31.             speed *= -1;
    32.         }
    33.     }
    34.  
    35.     void Destruir_()
    36.     {
    37.         Destroy(this.gameObject);
    38.     }
    39.  
    40.     private void OnCollisionEnter2D(Collision2D collision)
    41.     {
    42.         Destruir_();
    43.        
    44.     }
    45.  
    46.  
    47.  
    48. }
    And when i try to play the game i have an error that says: "MissingReferenceException: The object of type 'GameObject has been destroyed but you are still trying to access it." And when i see the hierarchy the object of bullet has been destroyed but i don't understand why doesn't appear again the bullet. I'm new at coding, probably i missing something but i don't know what.
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Why are you invoking Destruir_ in your start routine?
     
  3. JackReivaj

    JackReivaj

    Joined:
    Feb 7, 2021
    Posts:
    33
    I did this following a tutorial and he did it in that way. But are you saying i should put the invoke on the update maybe?
     
  4. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    No, it is just an odd way to do things. You might try temporarily commenting it out and seeing if it is the cause of the issue though.
     
  5. JackReivaj

    JackReivaj

    Joined:
    Feb 7, 2021
    Posts:
    33
    I was able to solve it this way:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Bullet : MonoBehaviour
    6. {
    7.  
    8. public float bulletSpeed = 15f;
    9. public Rigidbody2D rb;
    10. public Transform player_pos;
    11.  
    12. void Start()
    13.     {
    14.         player_pos = GameObject.Find("Player").transform;
    15.     }
    16.  
    17. private void FixedUpdate()
    18.     {
    19.         if (player_pos.position.x > this.transform.position.x)
    20.         {
    21.             rb.velocity = transform.right * bulletSpeed;
    22.         } else
    23.         {
    24.             rb.velocity = transform.right * -bulletSpeed;
    25.         }
    26.     }
    27.      
    28.     private void OnCollisionEnter2D(Collision2D collision)
    29.     {
    30.         Destroy(gameObject);
    31.      
    32.     }
    33.  
    34. }
     
    tuluu1968 likes this.