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
}

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: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn

There is a video there dedicated to if statements as well.