Reputation: 325
I'm currently making a game in Unity where I'm trying to get destroy the clones of a prefab only after they leave the view of the camera only after they already entered the view of the camera in the first place. However, for some reason, my code is instantly destroying the clones as soon as their instantiated. Does anyone know how I could solve this problem?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InteractControl : MonoBehaviour
{
Rigidbody2D rb;
GameObject target;
float moveSpeed;
Vector3 directionToTarget;
// Use this for initialization
void Start()
{
target = GameObject.Find("White Ball");
rb = GetComponent<Rigidbody2D>();
moveSpeed = 3f;
}
// Update is called once per frame
void Update()
{
MoveInteract();
OnBecameVisible();
}
/*void OnTriggerEnter2D(Collider2D col)
{
switch (col.gameObject.tag)
{
case "ColouredBall Highress":
BallSpawnerControl.spawnAllowed = false;
Destroy(gameObject);
target = null;
break;
case "Star":
Collision collision = new Collision();
break;
}
} */
void MoveInteract()
{
if (target != null)
{
if(ScoreScript.scoreValue > 3)
{
directionToTarget = (target.transform.position - transform.position).normalized;
rb.velocity = new Vector2(directionToTarget.x * moveSpeed,
directionToTarget.y * moveSpeed);
}
else
{
directionToTarget = new Vector3(0, -1, 0);
rb.velocity = new Vector2(0, directionToTarget.y * moveSpeed);
}
}
else
rb.velocity = Vector3.zero;
}
void OnBecameInvisible()
{
if (gameObject.tag == "ColouredBall Highress")
{
Destroy(gameObject);
}
if (gameObject.tag == "Star")
{
Destroy(gameObject);
}
}
void OnBecameVisible()
{
if (gameObject.tag == "ColouredBall Highress" || gameObject.tag == "Star")
{
OnBecameInvisible();
}
}
}
I tried to solve the problem by first requiring the objects to become visible in order for them to be able to get destroyed when out of view of the camera. In short I'm looking for the OnExit collider version for on OnBecameInvisible. I guess I could make the whole screen a collider and use on Exit collider on it. Does someone possibly also know how I could make a collider that covers the camera view?
Upvotes: 1
Views: 6068
Reputation: 357
You are calling OnBecameVisible every frame, so basically the first frame the object destroys itself. Deleting it from Update should do the trick, Unity already calls it for you.
Upvotes: 1
Reputation: 1889
It is because you call OnBecameInvisible()
from OnBecameVisible
. So when they are visible they get destroyed.
Also your code is doing so many redundant things you also call OnBecameVisible
from Update
etc.
You can simply use this instead:
Renderer m_Renderer;
void Start()
{
m_Renderer = GetComponent<Renderer>();
}
void Update()
{
//It means object is NOT visible in the scene if it is false is visible
if (!m_Renderer.isVisible)
{
Destroy(gameObject);
}
}
Note that: Destroying/Instantiating objects are not the best practices at this circumstance. Because it causes garbage collector to work a lot and it is expensive and can slow down your game. You can use object pooling instead. It basically puts object which are not in the field of view into an object pool and you keep their references and can use them later. Therefore, it is less costly than your method.
Upvotes: 5