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);
}
}
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()
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 :?
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);
}
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.