- Home /
Encaplsue function in if statement
Hey guys,
I want to disable the OnGUI() function, when the gameobject is not a Player. Since it stores the stats in this script and is used globally for damagecalculation, I don't want to do two different scripts.
So my question is, how could this be done?
if(isHuman){
function OnGUI(){
//show lifebar
}
}
this doesn't work.
But does this?
if(isHuman)
OnGUI();
function OnGUI(){
//show lifebar
}
Answer by GameVortex · Jan 28, 2014 at 12:01 PM
If statements needs to be inside of functions. You can not use them to exclude one of unitys event functions from running. You can use them inside of the function to exclude the content from running.
Easiest would be:
function OnGUI()
{
if(isHuman)
{
//show lifebar
}
}
I would also recommend going through some scripting tutorials: http://unity3d.com/learn/tutorials/modules/beginner/scripting
There is a video there dedicated to if statements as well.
thank you for your comment! But I read that calling OnGUI(), even when it is empty, costs performance, which I want to avoid.
That is negligible compared to using the actual GUI functionality. The performance loss is extremely $$anonymous$$imal.
To avoid the OnGUI from getting called:
Either don't have a script with OnGUI function on the GameObject if it is not a player, or use an alternative custom GUI system ins$$anonymous$$d of Unitys.
Your answer
Follow this Question
Related Questions
How do I check if a game object exists? 1 Answer
If Statement within OnGUI not working - any ideas? 4 Answers
GUI.skin not working inside if 2 Answers
rotating minimap compass according to character direction 1 Answer
GUI Placement Question. 1 Answer