There are many different ways for which you can get a reference to an object, or even a reference right to the render. That is definitely well documented. The answer to your question is really dependent on your application though. You can set a property on whatever game object the script that is checking renderer.isvisible, and then drag the object in question into the property inspector, for one… you can go find objects by tag using FindGameObjectByTag (or FindGameObjectsByTag to get a collection of multiple objects), you can find a render on a child object of the game object the script is in using GetComponentInChild… there’s several ways to find references to game objects, and from there you can use GetComponent… I’m really giving a broad answer here because the question is very broad.
This is actually a fundamental piece of Unity, so if you used Unity a lot I’m kind of confused why you wouldn’t know how to get the renderer component. I don’t mean that to be a jerk, I’m just wondering if maybe I misunderstood your question… if I did not, then it’s simple… get a reference to the GameObject in question using one of the many methods available (The one that makes sense to you, generally drag and drop in the property inspector method I described above, if you can do it that way), then just simply use GetComponent() … I think even yourGameObjectVariable.GetComponent().IsVisible
I hope that helps!
If I misunderstood your question, please clarify a bit.
I just meant how to do it in terms of the Renderer.isVisible property. Like do I replace “Renderer” with the variable that is referring to the game object? Do I get the Renderer of that object (like you suggested) and then do something like “Object.Renderer.isVisible” where Object is the variable referring to that object?
As I said, I never used Renderers in code, in my life. So I don’t know the syntax for controlling them.
Hope that narrows it down.
I’ve done a lot of referencing to objects and their components, but never anything with Renderers.
Then later you can check the IsVisible property like so:
if (myRendererVariable.IsVisible) {
// do stuff
}
The latter suggestion would be best if you are going to be referring to this renderer a lot (Ex. in an Update()). Store the renderer in a variable on Awake, or something, if you’re always working with that same renderer.
This isn’t tested code, sorry, but pretty sure that’s the gist. If that doesn’t work, let me know. Hopefully my explanation made sense!
“Note that the object is considered visible when it needs to be rendered in the scene. For example, it might not actually be visible by any camera but still need to be rendered for shadows. When running in the editor, the scene view cameras will also cause this value to be true.”
Let me know if you need clarification on that, but if you find that your object should not be registering as visible but is, that could be why. I believe there’s flags on the renderer (or somewhere?) in the property inspector for this sort of thing.
Also, I’m not sure what you need to do, but sometimes these triggers on the object in question can better achieve what you’re doing: OnBecameVisibleOnBecameInvisible
Unless the OP is working on something new, I do not think this will lead to what he’s hoping for in the big picture.
That property is probably set at the same time as the event(s) for visibility ; relating only to the camera’s frustum.
I could be wrong, but we were in a lengthy chat about how to determine if an object can be seen at all in the scene (vs. first offscreen, and/or completely culled by occlusion).**
I concur generally. However, if it is a camera setup (such as looking at a 2D plane of things) that always sees everything “of interest” in the frustum, then it might have the effect he is looking for, modulo things like a correct and parsimonious bounding box, etc.
But if the OP is looking to see “is this object poking his nose out of a hole far enough that I can headshot him or noseshot him?”, then this .isVisible absolutely will not get you what you want.
Well, I did the second solution, since I do want it to be in Update, but “ObjectRender” (the variable for that object renderer) and “isVisible” are all turning red. And I’m getting a “does not exist in the current context” error for “ObjectRender”, even though I did it exactly like you listed, even fixing some of the syntax.
I.e. :
Renderer myRendererVariable = yourGameObject.GetComponent<Renderer>;
to
Renderer myRendererVariable = yourGameObject.GetComponent<Renderer>();
I have occlusion culling, and I turned off both receiving and casting shadows for the object in question. So it shouldn’t render if it is fully behind another object.
Therefore I was hoping “isVisible” wouldn’t detect it, since it’s not being rendered at all. And only detecting it if it isn’t being culled.
btw, my game is 3D, and first person. Just in case that makes any difference.
Also, I heard that the scene cameras thing for “isVisible” was only back in 3.x.
//Outside of methods:
Renderer ObjectRender;
// Awake
ObjectRender = GetComponent<Renderer>();
// When you use it.
if ( ObjectRender.isVisible ) { /* your code */ }
Given what the other two posters mentioned, it might not be best to pursue this (sorry, I wasn’t aware of what you were using it for, like Methos), but … make sure “ObjectRender” is of type Renderer and has visibility in the Update function somehow (sounds like it doesn’t, perhaps?).
Example:
Renderer ObjectRender;
void Awake (){
ObjectRender = GetComponent<Renderer>();
}
void Update () {
if (ObjectRender.isVisible){
// do stuff
}
}
Also your class would need to inherit from Monobehaviour (in some way) as well in order to use GetComponent…
FYI, I just tried this in a project, and the code worked… and by worked, I just mean it compiled without errors. If what we’ve mentioned here is still highlighting red, might be best to paste code here.
That example is actually exactly what Methos just did. Creating a Renderer variable, and then getting its component “Renderer” which doesn’t make sense.
We posted at the same time, so I missed he had that answer already… but what about that piece of code doesn’t make sense? (fyi, i had a typo in mine that I’ve since fixed… named the initial variable wrong)
The Renderer variable is just a reference … to nothing… when you first declare it as “Renderer ObjectRender”. You need to then point it to the renderer, which is what the GetComponent function does. You could also do this using drag and drop on the property inspector too if you make the variable public (then just drag the prefab in question onto it). Maybe that’s what you meant by “doesn’t make sense”. We both were under the assumption you were getting the renderer component programatically.
public GameObject myObject; // this is between the class and methods.
void Awake () {
Renderer ObjectRender = myObject.GetComponent<Renderer>();
}
void Update () {
if (ObjectRender.isVisible) {
//some code
}
}
Oh I read that wrong then. I just didn’t understand it because there was no reference to an actual GameObject. I’ll try that example then. But I forgot that doing “Renderer” allows you to drag an object with a Renderer component.
You declared ObjectRender in the Awake function, therefore it only has scope in the Awake function…
Correct way:
public GameObject myObject; // this is between the class and methods.
Renderer ObjectRender;
void Awake () {
ObjectRender = myObject.GetComponent<Renderer>();
}
void Update () {
if (ObjectRender.isVisible) {
//some code
}
}
Oops, I missed the GameObject reference, so you’re right on that. If you do it the way I initially had it, it would look for a renderer on the gameobject where this script is on (which might actually be what you want anyways), but the new code I put up would work for a different gameobject. Sorry about that. Typing too fast here.
OMG IT WORKS!!!
I did that example that you (cstooch) and Methos posted!
Although there is one problem. When I hit Play, the renderer disappears from the field in the Inspector. I tried moving that reference from “Awake” to “Start” but same thing happens.