[Solved] Script Error: Script Accessing A Script

I am having probably a trivial error although it seems my code is correct. I want my GUI script to get the health variable from Health.js and display it to screen. I looked up some other threads about this, but from what I can tell it should work?

//GUIScript.js
function OnGUI () {
var healthScript : Health = GetComponent(Health);
var healthValue = healthScript.getHealth();
GUI.Box (Rect (10,10,100,40), "Health: "+healthValue);
}

When I run my script I am getting the following error referencing the above bold line of code:
NullReferenceException: Object reference not set to an instance of an object
GUIScript.OnGUI () (at Assets\Standard Assets\Scripts\GUIScript.js:2)
UnityEditor.EditorGUIUtility:RenderGameViewCameras(Rect, Rect, Boolean, Boolean)
UnityEditor.EditorGUIUtility:RenderGameViewCameras(Rect, Rect, Boolean, Boolean)
UnityEditor.GameView:OnGUI()
System.Reflection.MonoMethod:InternalInvoke(Object, Object[ ])
System.Reflection.MonoMethod:InternalInvoke(Object, Object[ ])
System.Reflection.MonoMethod:Invoke(Object, BindingFlags, Binder, Object[ ], CultureInfo)
System.Reflection.MethodBase:Invoke(Object, Object[ ])
UnityEditor.HostView:Invoke(String)
UnityEditor.DockArea:OnGUI()

Here is my Health Script so far, for reference.

// Health.js
var health: int;
var maxHealth: int;
var shield: int;
var maxShield: int;

 var spawn_object_on_death: boolean;
 var spawn_object: GameObject;

function Update()
{
	health = health +shield;
}
/**
 *Get/set methods
 */
//Health 
function getHealth()
{
return health;
}
function setHealth(h: int)
{
	health = h;
}

function addHealth(h: int)
{
	health += h;
	if(health > maxHealth)
		health = maxHealth;
}

function subtractHealth(h: int)
{
	health -= h;
	if(health <= 0)
		killObject();
}

//Shield 
function setShield(s: int)
{
	shield = s;
}

function addShield(s: int)
{
	shield += s;
}

function subtractShield(s: int)
{
	shield -= s;
}

function getShield()
{	
	return  shield;
}

function killObject()
{
	//Play death animation here if you need it.
	//If we are to spawn an object on death, do it now.
	if(spawn_object_on_death){
		 var deadObject = Instantiate(spawn_object, transform.position, transform.rotation);
		
	Destroy(gameObject, 0);
}
}

Any help would be greatly appreciated.

i would warn against initiating var’s in the onGUI function, as it is called every frame

i would suggest something like the following

//GUIScript.js
var healthScript;
var healthValue;

function Start(){
healthScript = this.GetComponent("Health");
}

function OnGUI(){
healthValue = healthScript.getHealth();
GUI.Box(Rect(10,10,100,40), "Health: " + healthValue);
}

Ah, yes, thank you for pointing that out to me! However I am still getting an error that:
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs)
GUIScript.OnGUI () (at Assets\Standard Assets\Scripts\GUIScript.js:11)
UnityEditor.EditorGUIUtility:RenderGameViewCameras(Rect, Rect, Boolean, Boolean)
UnityEditor.EditorGUIUtility:RenderGameViewCameras(Rect, Rect, Boolean, Boolean)
UnityEditor.GameView:OnGUI()
System.Reflection.MonoMethod:InternalInvoke(Object, Object[ ])
System.Reflection.MonoMethod:InternalInvoke(Object, Object[ ])
System.Reflection.MonoMethod:Invoke(Object, BindingFlags, Binder, Object[ ], CultureInfo)
System.Reflection.MethodBase:Invoke(Object, Object[ ])
UnityEditor.HostView:Invoke(String)
UnityEditor.DockArea:OnGUI()

This is refering to this line of code:

GUI.Box(Rect(10,10,100,40), "Health: " + healthValue);

So healthValue is not instantiated? But it seems it is supposed to be.

hmmmm

maybe try this at the top

var healthValue : int = 0;

That worked to get it to display however it is not reading the value I set in Unity’s inspector. And it gives the same error:

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, System.Object[] args, Boo.Lang.Runtime.DispatcherFactory factory) [0x00000]
Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Object[] args, Boo.Lang.Runtime.DispatcherFactory factory) [0x00000]
Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args) [0x00000]
UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType) [0x00000]
GUIScript.Start ()   (at Assets\Standard Assets\Scripts\GUIScript.js:7)

It seems it cannot perform the getHealth() function.

try healthValue= healthScript.health;

getters and setters arent really mandatory in unityscript, although i commend you for good programming habit, something i seem to have lost

Thanks, I figured that using getters/setters would make my reading of the code easier. That and since I have been programming in Java for 4 years in school that it is now habit to use those conventions.

I tried the adapted code you posted with no luck. Same error as my previous post. It seems that the Health script located on the same object as the GUI script is causing this issue. Both scripts are attached to the same object so I am not sure why I cannot access Health.health or the getHealth() function :?

did you put quotes around the

GetComponent("Health");

ive found it to work better than the object version of getComponent

im really just guessing at this point tho

Yes, this is my code. Oddly enough still not working:

//GUIScript.js
var healthScript;
var healthValue : int = 0;

function Start(){
healthScript = this.GetComponent("Health");
healthValue= healthScript.health;
}

function OnGUI(){
GUI.Box(Rect(10,10,100,40), "Health: " + healthValue);
}

The error occurs when calling the value from the Health object that is created during start. healthScript.health is giving the error that healthScript is an Object reference not set to an instance of an object. From all I have read and worked with I cannot understand why this isn’t working. Granted I just stared scripting last night and worked through some much simpler code first.

i just created a dummy scene - main camera has GUISCript and Health attatched to it with GUIScript being

//GUIScript.js 
var healthScript; 
var healthValue : int = 0; 

function Start(){ 
healthScript = this.GetComponent("Health"); 
} 

function OnGUI(){ 
	healthValue= healthScript.health; 
GUI.Box(Rect(10,10,100,40), "Health: " + healthValue); 
}

worked just fine

Seems my first person controller became corrupted. I deleted it and re-added it and reattached the scripts and now it does work. I have had this happen with geometry before but forgot about corrupted assets issues I have had previously. Perhaps this is a bug in Unity? Before I deleted and re-added the first person controller the scripts were attached to it wouldn’t even display any GUI, regardless of previously known working GUI scripts. The GUI literally stopped displaying after the last suggested code modification.

Thank you very much for your help and debugging!

np - glad its working - gl to you