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 /
This question was closed Nov 05, 2016 at 12:27 AM by WaywardKnight for the following reason:

Other: Error was identified and did not relate to the questions asked. I do not want to mark my own comment as an answer.

avatar image
0
Question by WaywardKnight · Nov 04, 2016 at 12:35 PM · gunfire

Object Not Dying

Hi Everybody,

I am sorry if this is a stupid question, I am new to Unity and trying to learn from some tutorials, etc. I have looked around for help before asking, and was not able to find anything.

I am trying to make a fps and am just building things up slowly. I am trying to make an object disappear when it gets show and its health reaches 0 or less.

The code if have for the gun is:

 var DamageAmount : int = 5;
 var TargetDistance : float;
 var AllowedRange : float = 15;
 
 function Update () {
     if(Input.GetButtonDown("Fire1")) {
 
         var Shot : RaycastHit;
             if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Shot)) {
                 TargetDistance = Shot.distance;
                     if (TargetDistance < AllowedRange) {
                         Shot.transform.SendMessage("DeductHealth", DamageAmount, SendMessageOptions.DontRequireReceiver);
                     }
             }
     }
 }

and The code of I have the object that should disappear is

 var EnemyHealth : int = 10;
 
 function DeductHealth (DamageAmount : int) {
     EnemyHealth -= DamageAmount;
 }
 
 function Update () {
     if (EnemyHealth <= 0) {
     Destroy(gameObject);
     }
 }

My understanding is the first gun shot that hits the target and is within the range of 15 should take thier health down to 5, the second one should take it to 0 which should trigger the 'destroy(gameObject)'.

Nothing seems to happen at all when I shoot. If I have the GunMechanics selected (located under FirstPersonCharacter) on the Inspector, I can see the 'Target Distance' value change so the shot is being acknowledged at least in part.

If you need any more informaiton to answer, please let me know.

Thanks in Advance.

Comment
Add comment · Show 1
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 JScotty · Nov 04, 2016 at 01:03 PM 0
Share

Try to edit this

 function DeductHealth

to

 public function DeductHealth


and

 Shot.transform.Send$$anonymous$$essage("DeductHealth", DamageAmount, Send$$anonymous$$essageOptions.DontRequireReceiver); 

to

 Shot.gameObject.GetComponent("Class name of enemy health").DeductHealth(DamageAmount);

1 Reply

  • Sort: 
avatar image
0

Answer by WaywardKnight · Nov 04, 2016 at 11:36 PM

Thanks for the quick response @JScotty. Unfortunatly, it didnt help.

but, now here is the stupid thing...I just figured put the issue....The object the gun script was attached too...it was rotated on the Y axes by 180 degrees...I don't know how that happend. But yeah, fixing that made it work perfectly...

Like I said, I greatly appreciate the quick response and I am sure if you could see the whole project you would have figured that out in no time.

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

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I prevent audio from restarting if it's being asked to play again? 2 Answers

Is there a script out there that actully works for shooting bullets? 2 Answers

**[SOLVED]** Gun not firing after certain amount of shots 1 Answer

Make bullet launch at center of screen 2 Answers

Reload clips on Gun-script not applied properly 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