- Home /
How do I destroy a game object (The enemy Goblin Game Object) upon entering a collision box? (JavaScript)
I have been trying to destroy a "Goblin" item that I have placed into my game, and my hopes are to have it be destroyed only when the player (which is named FPSController) is inside of the collision box (which I believe I have set up properly) and when they click the left mouse button(Fire1). I have a health system set up for the goblin, and I only want the goblin to be destroyed when its current health (gobHealth) is at or less than 0. Currently, the collision box DOES work with part of the script, the part that damages the player's health (curHealth) and plays the attacking animation, but for some reason the part about destroying the game object does not work. I have a Debug.Log set up to see if it works at all, and it does not show up, though I have no errors in the console until the player enters the collision box. The error says:
"NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cachKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)"
It's pretty darn long, sorry about that. Here is the script that I have written so far for the Goblin's health and damage effects. it is in JavaScript
static public var gobHealth : int = 50;
var maxHealth : int = 100;
var Animation : Animation;
var Goblin : GameObject;
function Start () {
}
function Update () {
if(gobHealth < 0 ) {
gobHealth = 0;
}
if(gobHealth > 100) {
gobHealth = 100;
}
}
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.tag == "Player"){
{
Goblin.GetComponent.<Animation>().Play("attack1");
yield WaitForSeconds(1.3);
PlayerHealth.curHealth -=10;
}
}
if(Vector3.Input.GetMouseButton("1")) {
gobHealth -= 100; //<----------This is the TOTAL attack on the goblin's health! IMPORTANT
Debug.Log("Hello");
}
}
if(gobHealth <= 0 ) {
Goblin.GetComponent.<Animation>().Play("death");
yield WaitForSeconds(1.3);
Destroy (Goblin);
}
//for(var i : int = 0; i < EnemyNumber; i++)
function OnTriggerExit (Other : Collider) {
Goblin.GetComponent.<Animation>().Play("combat_idle");
}
I apologize for the script looking rather odd, the copy and paste did not go very well. I have searched other forum topics to no avail. Please help in any way you can! It will be much appreciated!
Also, I don't particularly want to double post, but I have another question. Currently when the player enters the collision box, the script forces the player to lose 10 health after the animation attack1 plays out. The problem is that after that, the animation continues on a loop (as long as the player is in the collision box), but the health does not continue to drop. If you guys know anything about this please let me know. The full script is shown above, but the specific part that I have this set up in is:
function OnTriggerEnter (other : Collider) { if (other.gameObject.tag == "Player"){ { Goblin.GetComponent.().Play("attack1"); yield WaitForSeconds(1.3); PlayerHealth.curHealth -=10; } }
What is your 'Goblin' object? Is it a gameobject?
Goblin is a gameobject which I have dragged into the appropriate box created by the script
Answer by ShadyProductions · Mar 17, 2017 at 09:55 AM
I would check if your Goblin object is not null before doing any logic to it. And also that the Animation component is on the object.
Wrong, this is actually the correct way of getting a component in javascript.
Ah I had no idea that unityscript used a . in generics... ew.
Well, currently the collision box works to cause damage to the player and to play the Attacking animation, its just the dying animation and destroying the object that do not work.
Answer by toddisarockstar · Mar 17, 2017 at 04:46 PM
the part where you destroy your goblin looks to be outside of all your function brackets! ....so i would guess its only checking health one time when the game starts. Try moving it inside update or inside your trigger function!
Your answer
Follow this Question
Related Questions
Health below zero but shows negative numbers 1 Answer
Raycast Shoot Error 1 Answer
Object Not Recieving Damage 3 Answers
One enemy triggers all the enemies 2 Answers
How i make the player damage the enemy specific enemy he is colliding with 1 Answer