So I have this code
var Health = 100;
public static var cHealth = 100;
var HealthD : GUIText;
function Start() {
DisplayAmount();
}
function DisplayAmount () {
HealthD.text = ""+ cHealth;
}
function Update ()
{
// if you press Mouse Button
if (Input.GetMouseButtonDown(0)){ // if you press Left Mouse Button -> GetMouseButtonDown(0)
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // it sends a ray from Camera.main
if (Physics.Raycast(ray, hit)){ // if it hits something
if (hit.collider.gameObject.tag == "beep"){
cHealth += -10;
HealthD.text = ""+ cHealth;
if (cHealth <= 0){
if (hit.collider.GameObject.tag == "beep"){
DestroyObject(gameObject);
}
}
}
}
}
And when the enemies health gets to 0, I get the error:
“NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name)
UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)
raycast.Update () (at Assets/raycast.js:21)”
rather than the object being destroyed. I used to have the code like this:
var Health = 100;
public static var cHealth = 100;
var HealthD : GUIText;
function Start() {
DisplayAmount();
}
function DisplayAmount () {
HealthD.text = ""+ cHealth;
}
function Update ()
{
// if you press Mouse Button
if (Input.GetMouseButtonDown(0)){ // if you press Left Mouse Button -> GetMouseButtonDown(0)
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // it sends a ray from Camera.main
if (Physics.Raycast(ray, hit)){ // if it hits something
if (hit.collider.gameObject.tag == "beep"){
cHealth += -10;
HealthD.text = ""+ cHealth;
if (cHealth <= 0){
DestroyObject(gameObject);
}
}
}
}
but this ended up destroying the first person controller rather than the enemy. Could anyone tell me what is wrong?