Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mwester · Mar 16, 2017 at 09:12 AM · destroyenemydamage1st personhealth

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · Mar 16, 2017 at 01:37 PM 0
Share

What is your 'Goblin' object? Is it a gameobject?

avatar image Mwester · Mar 16, 2017 at 04:24 PM 0
Share

Goblin is a gameobject which I have dragged into the appropriate box created by the script

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IggyZuk · Mar 17, 2017 at 10:31 AM 0
Share

Wrong, this is actually the correct way of getting a component in javascript.

avatar image ShadyProductions · Mar 17, 2017 at 11:48 AM 0
Share

Ah I had no idea that unityscript used a . in generics... ew.

avatar image Mwester · Mar 17, 2017 at 04:17 PM 0
Share

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.

avatar image
0

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!
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges