Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 OctoSloths · Nov 04, 2014 at 03:32 PM · javascriptraycasttagsdestroy objecthealth

Enemy health script using raycast not destroying tagged object

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?

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 brentatplayful · Nov 04, 2014 at 05:49 PM 0
Share

I'm not really understanding this code. Is this on the enemies? It does NOT seem like it should be. It seems like a component on the controller? If so, DestroyObject(gameObject); is what's doing it.

Could you give some more info? It look like this script looks to see if you clicked on an gameObject with a tag of "beep". If so, reduce the players health by 10. If health <= 0. destroy the player gameObject.

avatar image OctoSloths · Nov 04, 2014 at 07:28 PM 0
Share

The script is on the first person controller, sorry for not clarifying. If I click on an object tagged with "beep" using raycast I get the error: "NullReferenceException: Object reference not set to an instance of an object " rather than it decreasing the object's health.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by billyjoetyler · Nov 04, 2014 at 05:50 PM

Well I am guessing that you have put this one you first person controller, if so you need to separate scripts one for the enemy, and one for the controller. The first script would be like this:

  function Start() 
         {
     
         }
         function Update ()
         {
         if (Input.GetMouseButtonDown(0)){
         var hit: RaycastHit;
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
         if (Physics.Raycast(ray, hit)){
         if (hit.collider.CompareTag("Beep")){
         hit.collider.gameObject.SendMessage("Damage");
        }
       }
      }
     }
 

Second script,(Enemy health), would be like this:

 private var Health = 100;
 
 function Start(){
 
 }
 function Update(){
 if(Health <= 0){
 Destroy(gameObject);
 }
 }
 function Damage(){
 Health -= 10;
 }

You could add the function that shows the health at the end.

This method shoots out a ray sees if it hits an object with the tag beep and if does sends a message Damage which the enemy picks up, then it minuses 10 health, then finally the enemy checks if it has any health left if not it destroys itself.

Comment
Add comment · Show 2 · 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 OctoSloths · Nov 04, 2014 at 07:36 PM 0
Share

Thank you so much!

avatar image billyjoetyler · Nov 04, 2014 at 07:39 PM 0
Share

On the enemy have you set the tag to "Beep"

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Make Body go limp after dead 1 Answer

Raycast Tag of Hit GameObject 1 Answer

how do i access an int var in a javascript and -= it from another javascript 1 Answer

Clicking on an Object to Make it the Variable Target 1 Answer

Setting Scroll View Width GUILayout 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