Trouble Accessing Variables from another gameobjedt

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);
    }

I might be wrong but it is probably because you are trying to access pScript during Awake() and it may not have initialized yet. Try changing Awake to Start to see if that helps at all. As for the speed there’s nothing in this script (unless it’s the DrawTexture but I doubt it) that would make it slow that I can see; you’re caching all the components properly etc from what I can tell, so it might be something somewhere else that’s causing this.

Finally, if I may, I recommend that you sort out your indentations; it will make your life a lot easier if you get into the habit of making sure that everything is indented properly. As it is now, at first glance it looks as though all those functions below are members of fearTexture (obviously they aren’t, but the indentation makes it look that way.)