- Home /
GetComponent ignoring if statement conditions
I'm trying to have objects become visible only when they fall within a particular area, however I'm having some trouble with GetComponent.
foreach( Plank plank in planks )
{
plank.CreateGameObjects();
plank.plankObject.transform.SetParent(world.transform);
if (plank.plankObject.transform.position.z < ymax)
if(plank.plankObject.transform.position.z > ymin)
if(plank.plankObject.transform.position.x > xmin)
if(plank.plankObject.transform.position.x < xmax)
plank.plankObject.GetComponent<Renderer>().enabled = true;
else
plank.plankObject.GetComponent<Renderer>().enabled = false;
}
I've checked the conditions and there doesn't seem to be a problem there (i.e. I had debug log print a message to the console when an object met the selection criteria and that went fine), but when I add in the lines to do with GetComponent I run into some problems. There are objects that do or don't have a mesh, and the ones that do/don't are consistent between runs, however whether or not an object has a mesh seems to be completely unrelated to the conditions I've set out.
Is this a common (relatively) problem with GetComponent, or am I just missing something obviously wrong?
Not sure where to put this, so I'll stick it here. I found a different way to achieve the same effect (different as in different to what I was trying to do here), so this question can probably be closed.
Answer by bogartrye · Jun 17, 2015 at 03:38 PM
When you say some objects don't have a mesh, I'm assuming they contain children that have meshes? Because if an object doesn't have a mesh, it won't be rendered in the first place and you wouldn't need to toggle its visibility. If that's the case, you would need to use GetComponentsInChildren to get all renderers and you can turn them on or off by looping through them. However, if you don't need to check objects' children because they are already contained in planks, just adding a simple test like what I have below would work.
if (plank.plankObject.GetComponent() != null) {
plank.plankObject.GetComponent<Renderer>().enabled = false;
}
Do the same test before setting the renderer.enabled to true as well.
Answer by Dave-Carlile · Jun 17, 2015 at 04:11 PM
Your else
statement only runs if all of the other previous statements are true, except x < xmax
. C# (and most modern languages) don't care about indentation. The most straightforward way of running this would be to use and
to combine all of your conditions into a single if
statement...
plank.CreateGameObjects();
plank.plankObject.transform.SetParent(world.transform);
Vector3 p = plank.plankObject.transform.position;
if (p.z < ymax && p.z > ymin && p.x > xmin && p.x < xmax)
{
plank.plankObject.GetComponent<Renderer>().enabled = true;
}
else
{
plank.plankObject.GetComponent<Renderer>().enabled = false;
}
Or even simpler - since the if
comparison returns a Boolean...
plank.plankObject.GetComponent<Renderer>().enabled =
(p.z < ymax && p.z > ymin && p.x > xmin && p.x < xmax)
Your answer
Follow this Question
Related Questions
Renderer.enable for GameObject[]? 3 Answers
animation.speed does not work when disabling/enabling renderer 1 Answer
Gameobject with many child objects disable mesh not working. 0 Answers
Keypress.ismine? 0 Answers
Cannot select Animation Event 3 Answers