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
}
}
}
}
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.
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: