- Home /
half of "if" argument being ignored by Unity
Hi guys, I've got a weird problem, and either I'm being totally daft and missing the obvious, or Unity's misbehaving on me. I'm trying "isVisible" for the first time - I have a herd of charactercontrolled characters that burst through the scene when triggered, follow some simple waypoints, then leave. Their (homemade) pathfinding is pretty good but there is sometimes a straggler that gets lost, so I put the following code as a countdown from "secondTimer", to make it disappear if not in view: secondTimer -= Time.deltaTime;
if(secondTimer < 1 && !transform.renderer.isVisible)
{
Destroy(gameObject);
}
However, even when there's a good 15 seconds or more on the secondTimer, if you look away the characters with the script attached totally disappear. It's as though the first part of the argument is completely ignored, and just working on the "isVisible" part.
Is there something I don't know about isVisible, am I having a wibble moment, or is this some kind of glitch? Anyone able to help please? Thanks Ian
Unity never misbehaves, and it's always your code. Really. :) For basic logic like this anyway; not that Unity is bug-free. You can trivially confirm this by changing the code to:
{
Debug.Log ("Destroying... secondTimer = " + secondTimer);
Destroy (gameObject);
}
There's nothing about isVisible that could have any effect on evaluating an unrelated bool. Also, because of short-circuiting, it won't even be checked unless secondTimer is less than 1 first. The most productive thing you can do is always assume your code is wrong, and use basic debugging techniques to narrow down the causes.
Eric - I've not had to really play with Debug.Log before - but that was very helpful, thanks. I think you've made my job easier in the future! Somewhere down the line my public variable secondTimer had got screwed up, so no matter how I changed my code in UnityScript, nothing was changing in-gane. Sods law says the one I picked at random to check was O$$anonymous$$. Unity is working as advertised, as I suspected, and yes I was being moronic. Thanks for setting me straight, and showing me something useful for the fuyure 8-)
Answer by belvita · Nov 29, 2013 at 07:32 PM
use this instead of !transform.renderer.isVisible):
//disable it from view
gameObject.renderer.enabled = false;
//also disable the gameobjects collider
gameObject.collider.enabled =false;