Hello,
I am trying to create a simple GUI script that holds all of the GUI elements for the game. I am starting with just a player health bar, which requires me to call in variables from the “PlayerScript”. I’m using a .Getcompenent to do this. But I either get a "behavior null or not found’ or the game throws back no errors, but runs really slowly. Below is my code. Where am I making the mistake? Playerfear is a variable from the script: PlayerController.
Sidenote, I realize that none of this was displaying on screen, so I created a guicontroller game object, placed it in my scene, and attached the script. The GUI texture, nor the box were visible, and I kept getting “Null Errors” I’m not sure why. Any help you can provide is greatly appreciated.
public var pScript: PlayerController;// since this is pragma strict the type of varible must be stated strictly. the varible will not be a "component" persae,but
// a type of compontent called PlayerController. long story short, if you do a pragma strict, the code will be more strict
//in typesetting of variables. Don't do pragma strict if you aren't comfy with this.
public var fearTexture: Texture2D ;
function Awake () {
//removed var from infront of pScript since we declared it already
pScript= gameObject.FindWithTag("Player").GetComponent(PlayerController); //First find the player object and assign it to a variable
print(pScript.playerFear);
}
function OnGUI () {
GUI.DrawTexture(Rect(10,10,(Screen.width/2)*(( pScript.playerFear+1.0)/(1.0*pScript.maxFear+1)),20), fearTexture, ScaleMode.StretchToFill, true, 10.0f);
//GUI.Box(new Rect ( 10, 10, (Screen.width/2),20),(pScript.playerFear)+"/"+(pScript.maxFear) ); //This is its on rectangle
};
function Update()
{
print(pScript.playerFear);
}