- Home /
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
}
}
}
}
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.
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
So as long as while the game is running, and the ball is rendered then it will be true?
Unity also has a problem with the "renderer" part of the code.
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.
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?
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;
Your answer
Follow this Question
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