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

OnBecameInvisible with Instantiate objects?

Discussion in 'Scripting' started by Zefrus, Jan 22, 2021.

  1. Zefrus

    Zefrus

    Joined:
    Dec 3, 2019
    Posts:
    14
    Noob coder. I'm trying to destroy my bullet object once it exists outside the view of the camera. I first tried to create a new void, OnBecameInvisible, and destroy it, but I am finding it difficult to get the void to recognize the GameObject, bullet. Here is my current functioning code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerShoot : MonoBehaviour
    6. {
    7.  
    8.     public Transform firePoint;
    9.     public GameObject bulletPrefab;
    10.     public float bulletForce = 200f;
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         //delete fluff and use GetbuttonDown to revert
    16.          if(Input.GetButtonDown("Fire1"))
    17.             {
    18.                 Shoot();
    19.             }
    20.     }
    21.  
    22.     void Shoot()
    23.     {
    24.         GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
    25.         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
    26.         rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    27.         Destroy(bullet, 1); //want to change this to destroy once off camera
    28.     }
    29. }

    Is there any way to pass bullet into a 'void OnBecameInvisible'? I tried to add this void below thew void Shoot and couldn't get it to work. This is what I tried versus the code above.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerShoot : MonoBehaviour
    6. {
    7.  
    8.     public Transform firePoint;
    9.     public GameObject bulletPrefab;
    10.     public float bulletForce = 200f;
    11.     private GameObject bullet;
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         //delete fluff and use GetbuttonDown to revert
    17.          if(Input.GetButtonDown("Fire1"))
    18.             {
    19.                 Shoot();
    20.             }
    21.     }
    22.  
    23.     void Shoot()
    24.     {
    25.         bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
    26.         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
    27.         rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    28.      
    29.     }
    30.  
    31.     void OnBecameInvisible()
    32.     {
    33.         Destroy(bullet);
    34.     }
    35. }
    36.  
     
    Last edited: Jan 22, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Can you just put a timed death sentence on it? You know, something simple like:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TimedDeathSentence : MonoBehaviour
    5. {
    6.     public float ageLimit = 2.0f;
    7.  
    8.     IEnumerator Start()
    9.     {
    10.         yield return new WaitForSeconds(ageLimit);
    11.         Destroy(gameObject);
    12.     }
    13. }
     
  3. Zefrus

    Zefrus

    Joined:
    Dec 3, 2019
    Posts:
    14
    Yeah, that's what I currently have. I'm creating a rail shooter where I don't want the player to be able to begin doing damage to enemies who have not appeared yet.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    That's totally reasonable.

    What about using objects anchored just offscreen as the bounds and checking when you go beyond them? Is it a fixed scrolling shooter kinda deal? If the player is actually moving through space, perhaps parent the bounds to him, or just use raycasts from the camera corners to update them?

    It seems for such a simple thing as bounds check I would tend to not reach for an API that might not be as well-documented as one would like...
     
  5. Zefrus

    Zefrus

    Joined:
    Dec 3, 2019
    Posts:
    14
    Yeah, you make good points. My player is bound on the y-axis but can freely move on the x-axis, aka left to right. I suppose if I were to do a bound method then the camera bounds would probably be best.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    I just don't like introducing needless dependencies for something so simple. :)

    I've made a few scrolling shooters in my life too so I feel I can speak.

    OTOH I've never even looked on the OnBecameInvisible thing but just hearing it scares me. "What does invisible even mean?" etc.