OnBecameInvisible, not working on my object, whats wrong in here ??

Let me describe more, my script instantiates 2 objects [Prefabs]

  1. bottle [Script attached to bottle has OnBecameInvisible function]
  2. character [Script attached to character has OnBecameInvisible function]

It is working on bottle, but not on my character, see the script on bottle and character below , see the images too::


Script on Bottle

using UnityEngine;
using System.Collections;

public class bottleCollector : MonoBehaviour {
	
	public static int _currentBottlesCollected=0;
	

	// Use this for initialization
	void Start () {
		//do -90 to make it straight
	transform.Rotate(-90, 0, 0);
	}
	
	// Update is called once per frame
	void FixedUpdate () {
		float a;
		float b;
		a = characterScript.hero_z_position;
		b = transform.position.z;
		//Debug.Log(a);
	//transform.Translate(-Vector3.forward * Time.deltaTime);
		if(characterScript.hero_z_position>transform.position.z){
			Debug.Log("bottle is at the back");
			Destroy(gameObject);
		}
	}

	void OnTriggerEnter(Collider other) {
		if(other.gameObject.tag == "Player"){
			_currentBottlesCollected=_currentBottlesCollected+1;
			scoreHandler.currentgameBottlesCollected = _currentBottlesCollected;
		//	Debug.Log(scoreHandler.currentgameBottlesCollected);
		//	Debug.Log(".."+_currentBottlesCollected);
			Destroy(gameObject);
    }}
	
	 void OnBecameInvisible() {
		Debug.Log("Invisible bottle");
        Destroy(gameObject);
    }
}

using UnityEngine;
using System.Collections;

public class peopleMovement : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	transform.Rotate(0, 180, 0);
		Debug.Log("Arrived");
	}
	
	// Update is called once per frame
	void Update () {
		float a;
		float b;
		a = characterScript.hero_z_position;
		b = transform.position.z;
		//Debug.Log(b);
	//transform.Translate(-Vector3.forward * Time.deltaTime);
		if(characterScript.hero_z_position>transform.position.z) {
			Debug.Log("i am behind the hero");
			Destroy(gameObject);
		}
	
	}
	
	 void OnBecameInvisible() {
		Debug.Log("Invisible man");
        Destroy(gameObject);
    }
	
	
}

I see the Debug.Log message ā€œInvisible bottleā€ in the console, but i dont see the Debug.Log ā€œInvisible manā€. What is wrong ??
Its been 5 hours i am fiddling with it

2 Likes

Just guessing here but reading the docs about OnBecameInvisible it appears to me that there needs to be a renderer component on the same object as the script which implements OnBecameInvisible which isnt the case for your ā€˜drunk’ object.

21 Likes

you are right,

this was the issue…thanks resolved.

1 Like

Arterie, thanks, solved it for me too. I use an empty gameobject to parent all my models to but I put my scripts at the top empty gameobject, so no mesh renderer on it. Adding a render component did the trick.

1 Like

With me, the reason that OnBecameInvisible() didn’t trigger was that while the Game view camera lost vision of the object, the Scene view camera continued to see it. So i had to hide the scene view tab, and leave only the Game view tab visible in the editor.

25 Likes

Thanx lucianv, had the same problem. Interesting that Scene tab influents on onBecameInvisible ().

2 Likes

Solve me too, simply add a render component as said above will do the trick

seems so obvious xD

This was true for me too. I was observing both Scene and Game View side by side and OnBecameInvisible() was not calling. I had to change my Editor layout settings into looking at a single view at a time and it started to work.

Two solutions here ppl !
take care :smile:
I had to apply both to work ;p
check ( @ardo314 ) and ( @lucianv ) answers

2 Likes

Hiding my scene View solved my issue. Why would it count the scene view camera? You would think Unity is smart enough to tell the difference.

Thanks, I would have been pulling my hair out for hours!

1 Like

Been a long time but speaking for Unity version 2021.3.2f1, I looked away from the object in the scene view, disabled it’s shadows, and disabled occlusion culling and the issue was solved.

1 Like