• 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 chewdri · May 06, 2013 at 05:35 PM · enemyshothealth

help with if(hit.gameObject.tag)

Hey I try to add health at an ennemy

here is my enemy's scrip

   var vie : int = 500;
  
    function OnCollisionEnter (hit : Collision)
 {
        if(hit.gameObject.tag == "bullet")
      {
      vie -= 25;
      Debug.Log (vie);
      if (vie <= 0){
      Destroy(gameObject);
       }
       }
 }

and here's the shot script on my character

 var bullet : Transform;
     var speed = 650;
     var Speed = 10;
     var spawnPoint : Transform;
     var rapidFire = false;
     var counter = 0.0;
     var rateOfFire = 0.25;
     var ennemydeux : Transform;
      
     function Update ()
     {
     CheckClick();
     }
      
     function CheckClick()
     {
     if(Input.GetButtonDown("Fire1") || (rapidFire && Input.GetButton("Fire1")))
     {
     var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     // you can set the 100 to whatever distance you want :P
     if (Physics.Raycast(ray,hit,100))
     {
     var enemy = hit.transform.GetComponent(ennemy);
     if(hit.transform.GetComponent(ennemy))
     {
     transform.LookAt(ennemydeux);
     HandleFiring();
     }
     }
     }
     }
      
     function HandleFiring()
     {
     if(rapidFire)
     {
     counter += Time.deltaTime;
     // I'm going to assume RateOfFireCounter is a variable that you've not shown here? Otherwise this won't do anything for you...
     if(rateOfFire)
     {
     var shotRapid =Instantiate(bullet, spawnPoint.transform.position, Quaternion.identity);
     shotRapid.rigidbody.AddForce(transform.forward * speed);
     shotRapid.transform.LookAt(ennemydeux);
     shotRapid.transform.Translate(Vector3.forward*Speed*Time.deltaTime);
     Destroy((shotRapid.gameObject), 2);
     
     Counter=0;
     }
     }
     else
     {
     var shot = Instantiate(bullet, spawnPoint.transform.position, Quaternion.identity);
     shot.rigidbody.AddForce(transform.forward * speed);
     shot.transform.LookAt(ennemydeux);
     shot.transform.Translate(Vector3.forward*Speed*Time.deltaTime);
     Destroy((shot.gameObject), 2);
     }
     
     


And the problem is, when the bullet hit my enemy, his life stay at 500. When I remove the part of my script: == "bullet" the enemy lose life when anything hit him.

Comment
Add comment · Show 6
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 roojerry · May 06, 2013 at 05:46 PM 0
Share

are you 100% sure the tag of your bullet game object is exactly "bullet"?

avatar image Dave-Carlile · May 06, 2013 at 05:47 PM 0
Share

Right before that if, add Debug.Log("tag=" + hit.gameObject.tag);

What tag does it show when the bullet hits?

avatar image chewdri · May 06, 2013 at 06:01 PM 0
Share

Nvm my bullet tag wasnt exactly "bullet" ! :D

avatar image ExTheSea · May 06, 2013 at 06:51 PM 0
Share

So now you should either close the question using the button near the edit, more, ... below the question or one of you the ones who commented convert their question to an answer so that you can accept it.

avatar image chewdri · May 06, 2013 at 07:29 PM 0
Share

$$anonymous$$y first problem is solved. But now, when I kill my ennemy I have the error "The object of type "Transform" has been destroyed but you still try to access it"

How can I do ?

Show more comments

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Lose health on collision 1 Answer

(C#) Enemy health and take damage from bullets 1 Answer

I made my code so that if i hit an enemy its health will go down but when i hit the enemy the health doesnt go down. please help 1 Answer

I use this script, but the enemy lose health if i don´t target him. 1 Answer

shooting script wont damage enemy 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