Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by jessica1986, May 5, 2013.

  1. jessica1986

    jessica1986

    Joined:
    Feb 7, 2012
    Posts:
    621
    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::

    $1.jpg
    Script on Bottle
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class bottleCollector : MonoBehaviour {
    5.    
    6.     public static int _currentBottlesCollected=0;
    7.    
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         //do -90 to make it straight
    12.     transform.Rotate(-90, 0, 0);
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void FixedUpdate () {
    17.         float a;
    18.         float b;
    19.         a = characterScript.hero_z_position;
    20.         b = transform.position.z;
    21.         //Debug.Log(a);
    22.     //transform.Translate(-Vector3.forward * Time.deltaTime);
    23.         if(characterScript.hero_z_position>transform.position.z){
    24.             Debug.Log("bottle is at the back");
    25.             Destroy(gameObject);
    26.         }
    27.     }
    28.  
    29.     void OnTriggerEnter(Collider other) {
    30.         if(other.gameObject.tag == "Player"){
    31.             _currentBottlesCollected=_currentBottlesCollected+1;
    32.             scoreHandler.currentgameBottlesCollected = _currentBottlesCollected;
    33.         //  Debug.Log(scoreHandler.currentgameBottlesCollected);
    34.         //  Debug.Log(".."+_currentBottlesCollected);
    35.             Destroy(gameObject);
    36.     }}
    37.    
    38.      void OnBecameInvisible() {
    39.         Debug.Log("Invisible bottle");
    40.         Destroy(gameObject);
    41.     }
    42. }
    43.  

    $2.jpg
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class peopleMovement : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.        
    9.     transform.Rotate(0, 180, 0);
    10.         Debug.Log("Arrived");
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         float a;
    16.         float b;
    17.         a = characterScript.hero_z_position;
    18.         b = transform.position.z;
    19.         //Debug.Log(b);
    20.     //transform.Translate(-Vector3.forward * Time.deltaTime);
    21.         if(characterScript.hero_z_position>transform.position.z) {
    22.             Debug.Log("i am behind the hero");
    23.             Destroy(gameObject);
    24.         }
    25.    
    26.     }
    27.    
    28.      void OnBecameInvisible() {
    29.         Debug.Log("Invisible man");
    30.         Destroy(gameObject);
    31.     }
    32.    
    33.    
    34. }
    35.  
    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
     
    Deleted User and osamansr2o1oo like this.
  2. ardo314

    ardo314

    Joined:
    Jul 7, 2012
    Posts:
    345
    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.
     
    saikowshik82, zeimhall, ow3n and 17 others like this.
  3. jessica1986

    jessica1986

    Joined:
    Feb 7, 2012
    Posts:
    621
    you are right,

    this was the issue...thanks resolved.
     
    saikowshik82 likes this.
  4. Ebkac

    Ebkac

    Joined:
    Feb 3, 2012
    Posts:
    62
    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.
     
    AkatoshIR likes this.
  5. lucianv

    lucianv

    Joined:
    Oct 7, 2014
    Posts:
    5
    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.
     
  6. Jakhongir

    Jakhongir

    Joined:
    May 12, 2015
    Posts:
    37

    Thanx lucianv, had the same problem. Interesting that Scene tab influents on onBecameInvisible ().
     
    nfk2018 and Sharknado1 like this.
  7. GunLengend

    GunLengend

    Joined:
    Sep 24, 2014
    Posts:
    54
    Solve me too, simply add a render component as said above will do the trick
     
  8. Broewly

    Broewly

    Joined:
    Aug 25, 2014
    Posts:
    1
    seems so obvious xD
     
  9. umerabc

    umerabc

    Joined:
    Apr 21, 2017
    Posts:
    1
    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.
     
  10. osamansr2o1oo

    osamansr2o1oo

    Joined:
    Sep 10, 2015
    Posts:
    9
    Two solutions here ppl !
    take care :D

    I had to apply both to work ;p

    check (@ardo314) and (@lucianv) answers
     
    LeandroExHuMeD and codestage like this.
  11. NeilLondon

    NeilLondon

    Joined:
    Jun 30, 2020
    Posts:
    2
    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!
     
    erayaybek likes this.
  12. WindwalkerDM

    WindwalkerDM

    Joined:
    Feb 14, 2015
    Posts:
    10
    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.
     
    Gigi_Ponce likes this.