Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by luniac · Dec 13, 2014 at 06:56 PM · spriteonbecamevisible

OnBecameVisible() called out of nowhere...

I have a 2d sprite#1 that appears on screen at which point OnBecameVisible() is called...

then i have the sprite#1 animate while calling a function inside another sprite#2, then i destroy sprite #2 at which point sprite#1 inexplicably calls OnBecameVisible() again!!!

But it never called OnBecameInvisible()!

I have no idea why this could be happening! anyone have any idea?

It only happens as soon as i destroy the sprite#2 but its simply a damage function that has NOTHING to do with rendering sprite#1 and as i said OnBecameInvisible() is never called!

In fact after testing i noticed that even destroying sprite#2 before sprite#1 calls the damage function, OnBecameVisible() is still called! And it's only called IF OnBecameVisible() is called initially when the sprite first appears, so if sprite#1 was off screen and i destroy sprite#2 then nothing happens.

UPDATE: So i did more testing, Even if i completely remove any connection between sprite#1 and sprite#2, so no references, no functions calls, NOTHING AT ALL, and i call Destroy(sprite#2) after sprite#1 called OnBecameVisible() the first time, it STILL calls OnBecameVisible() AGAIN on sprite#1.... wtf!?!?

UPDATE: Apparently ANY sprite that exists and is visible on the screen gets OnBecameVisible() called when sprite#1 is destroyed. AMAZING!

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mmmpies · Dec 13, 2014 at 09:09 PM 0
Share

I'm not sure I can help but if you post your code you're more likely to get answers. Very hard for anyone to help if they can't see what code is running.

avatar image luniac · Dec 14, 2014 at 01:14 AM 0
Share

yea that's too tricky cause its multiple long scripts... itll just confuse everyone.

All i can say for certain is that after thorough testing, if i have any sprite on screen that can be completely unrelated to sprite#1 and i destroy sprite#1, OnBecameVisible() is called for all those sprites as soon as destroy is called. It's the weirdest thing i've ever seen...

The only connection between them is the fact that all the sprites except sprite #1 are from the same sprite sheet which i don't think would affect anything.

I just don't understand how 1 sprite completely unrelated to another sprite can cause that sprite to call OnBecameVisible()... i just dont get it...

avatar image Crazydadz · Dec 18, 2014 at 07:57 PM 0
Share

Did you know that the camera of the Scene view will call OnBecameVisible on object too? So even if your in-game camera isn't seeing the object, but the scene view still render it, you will never get a OnBecameInvisible call. $$anonymous$$aybe, that is not the issue, but it could help you find the issue.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Crazydadz · Dec 18, 2014 at 09:40 PM

Ok it seems to be a Unity bug, I'm getting the same issue. I'm creating 3 cubes. If I'm disabling the renderer of one of them (don't disable the renderer of the last created one, it won't work) this way:

 if(Input.GetKeyDown(KeyCode.E))
  renderer.enabled = false;

it calls nowhere OnBecameVisible on the other cube (but only on the last created one). Not supposed to call OnBecameVisible anyway.

So you should file a bug.

A way you could avoid that issue is to double check your visibility this way

     private bool _isVisible;
 
     private void OnBecameVisible()
     {
         if (_isVisible)
             return;
 
         _isVisible = true;
         //Do what you want;
     }
 
     private void OnBecameInvisible()
     {
         if (!_isVisible)
             return;
 
         _isVisible = false;
         //Do what you want;
     }


You should create a basic project with cube to demonstrate it before filing a bug. I'll do the same when I'll have time.

Thank you!

David

Edit: OnBecameInvisible won't be called if you Destroy the gameObject (I'm not sure of that). You could use the function

 private void OnDestroy()
 {
 //Do what you want
 }

to avoid this.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image luniac · Dec 18, 2014 at 10:54 PM 0
Share

Hey thanks a lot for providing help :)

Yea i just had a check to make sure the function wasn't called already but I figured out a different approach to solve my problem without relying on OnBecomeVisible(), a pretty elegant one as well at least for me personally lol.

If you submit a bug report ill gladly +1 it.

avatar image Crazydadz · Dec 19, 2014 at 10:33 PM 0
Share

Hi, I'm glad you found a different approach :).

you can vote for the issue here: Issue Tracker

Thank you :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Rotate 2D Sprite Towards Moving Direction? 0 Answers

Sprites Become Invisible (SOLVED) 4 Answers

How do you animate or import particles in Unity? 0 Answers

hide animated Sprite by code (SpriteRenderer) 2 Answers

SIGILL error when adding new png files 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges