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 kbeaud · May 26, 2013 at 10:55 PM · c#animationrenderingif statement

How to check if an object is rendered?

So I have a script that it wants to check if an object is rendered then activate an animation... So here is my script. I believe most of it is right except for the part to check if an object is rendered.

 using UnityEngine;
 using System.Collections;
 
 public class Ball_Throw : MonoBehaviour {    
     
     private object bouncyBall;
     // Use this for initialization
     void Start () {
         bouncyBall = GameObject.FindGameObjectsWithTag("Bouncy Ball");
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetKeyUp(KeyCode.A)) {                                                    //Checks to see if the "A" key has been pressed
             if(bouncyBall.render.enabled == true) {                                    //Then checks to see if the "Bouncy Ball" has been rendered
                 animation.Play();                                                        //If all things comply then activates an animation to throw the ball
             }
         }
     }
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by peter.parker · Nov 13, 2014 at 11:57 AM

Apply the following script on the gameobject whom you want to check

 using UnityEngine;
 using System.Collections;
 
 public class CheckObjectRender : MonoBehaviour {
 
     private Transform target;
 
     void Start () {
         target = this.transform;
     }
     
     void Update () {
         Vector3 viewPos = Camera.main.WorldToViewportPoint(target.position);
         if ( (0f <= viewPos.x && viewPos.x <= 1F) && (0f <= viewPos.y && viewPos.y <= 1F) && (Camera.main.transform.position.z < this.transform.position.z) ) {
             print("target is in the camera! ----- ");
         } else if ( (0f > viewPos.x) ) {
             print("target is on the left side!");
         } else if ( (viewPos.x > 1F) ) {
             print("target is on the right side!");
         } else {
             print("target is NOT in the camera!");
         }
     }
 }

Remember: Render checking is in the Update which is expensive.

Comment
Add comment · 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
0

Answer by robertbu · May 26, 2013 at 11:07 PM

You have several issues with your code here. First FindGameObjectsWithTag() returns an array of all game objects that have that name. If you only have one "boundy Ball" in your scene, then you want ot use GameObject.Find() instead. If you have several, then you need to declare bouncyBall as:

 private GameObject[] bouncyBall;

...and you will need to do a for() loop to cycle through all the game objects in the bouncyBall array.

And, to access the renderer of a game object, use 'renderer' not 'render'.

As for seeing if a ball is rendered, the easiest is to the Renderer.isVisible flag. But this will return true if any camera sees the object. In the editor, this will include the Scene camera. If you have multiple cameras, things get more complicated. Look at this post for a link to the references for the many ways to solve this problem:

http://answers.unity3d.com/questions/8003/how-can-i-know-if-a-gameobject-is-seen-by-a-partic.html

Comment
Add comment · Show 7 · 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 kbeaud · May 27, 2013 at 03:34 PM 0
Share

So as long as while the game is running, and the ball is rendered then it will be true?

avatar image kbeaud · May 27, 2013 at 03:36 PM 0
Share

Unity also has a problem with the "renderer" part of the code.

avatar image robertbu · May 27, 2013 at 03:39 PM 0
Share

If you only have a single camera then isVisible will only be true when any part of the renderer.bounds is visible to that camera. $$anonymous$$y guess is you can test this in the editor by turning the Scene camera away from the objects you are testing. I'm not sure what you mean by "Unity...renderer." You'll have to post the code.

avatar image kbeaud · May 27, 2013 at 03:42 PM 0
Share

No the console is saying: Assets/Game/Scripts/Repeated/Ball_Throw.cs(15,39): error CS1061: Type object' does not contain a definition for renderer' and no extension method renderer' of type object' could be found (are you missing a using directive or an assembly reference?)

Also is there a way to check if it's just straight up rendered no matter what? Like whether a camera sees it or not?

avatar image robertbu · May 27, 2013 at 05:20 PM 0
Share

Seeing the code would help. This error occurs because Unity does not have the type of the object you are using at compile time. You can double click on the error and the variable/error will be highlighted in $$anonymous$$ono. The fix is usually to typecast the variable. Example:

 var goOther : GameObject;
Show more comments

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

14 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

Related Questions

How to check if animation is playing? C# 1 Answer

Conflicting Animator SetTrigger 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

What is the proper way to wait for an Animator Controller to update? 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