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 ElfElix · May 08, 2014 at 07:28 AM · 3d

Damage script not working?

Following this Guide: https://www.youtube.com/watch?v=FKT7jIW6ORU

Everything seems correct, but the enemy (capsule) simply doesn't lose health nor die. I've been at it for an hour or two and tried all I could, but nothing seems to be in my favor.

the two scripts are:

=====================================================

MeleeSystem Below

 #pragma strict
 
 var TheDamage : int = 50;
 var Distance : float;
 var MaxDistance : float = 1.5;
 
 function Update () {
     
     if (Input.GetButtonDown("Fire1")) 
     {
     var hit : RaycastHit;
     if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit))
         {
         
             Distance = hit.distance;
             if (Distance < MaxDistance)
             {
                 hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
             }
         }
     }
     
 }


================================================================================= EnemyLogic:

 #pragma strict
 
 var Health = 100;
 
 function ApplyDamage (TheDamage : int)
 {
     Health -= TheDamage;
     
     if(Health <= 0)
     {
         Dead();
     }
 }
 
 function Dead()
 {
     Destroy (gameObject);
 }







EDIT: I made some tweaks the code(s) will be listed for paste bin (it takes up spaces) below. I think I have fixed some stuff, but now I have only one ERROR, it reads as follows.

Assets/EnemyLogic.js(7,12): BCE0034: Expressions in statements must only be executed for their side-effects.

EnemyLogic MeleeSystem

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mads_AE · May 08, 2014 at 09:17 AM

I'm not that much into JavaScript, but from a C# perspective i would probably have the Function in the EnemyLogic public (is that a thing in JS), and i would get the component of the enemy script instead of sending a message, last thing is i would send the message to the game object and not the transform.

I hope one of those things help you out, i know way to well how it is to be stuck with something that seems easy and effortless.

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 ElfElix · May 08, 2014 at 10:59 PM 0
Share

Hi $$anonymous$$ads_AE, I tried what you suggested by changing it to the following line of code

 public function ApplyDamage (TheDamage : int)

I don't get debug errors with the script, but I think it's something wrong with the EnemyLogic, because the $$anonymous$$eleeSystem still registers the distance whenever I click an object within specified distance of me. So, I believe it is either the EnemyLogic or the following lines of code in the $$anonymous$$eleeSystem Script

 {
           hit.transform.Send$$anonymous$$essage("ApplyDamage", TheDamage, Send$$anonymous$$essageOptions.DontRequireReceiver);
          }


I've rewatched the guide many times over now and cannot spot any noticeable difference(s) (or any at all really between my code and his.

avatar image Mads_AE · May 13, 2014 at 07:24 AM 0
Share

I think if you save the script you want to save to into a var, like this

var script : EnemyLogic; script = hit.gameObject.getComponent(EnemyLogic); script.ApplyDamage(TheDamage);

Im sorry if some of the camel casing is wrong as JS is not m

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Finding the bounds of a grouped model 2 Answers

Move object towards mouse in full 3D space 2 Answers

Begginer Jumping javascrpit problem. 1 Answer

C# Script error (error CS8025: Parsing error) 0 Answers


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