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 addhennequant · Aug 31, 2012 at 03:34 AM · damagecollidekillhealth

Help with "Kill" script

So I have this script that minuses 10 Health from my main Player. But the problem is he doesn't die when it reaches 0, and i am having trouble scripting it. Here is the script:

function OnTriggerEnter (col : Collider) { var player : FPSPlayer = col.GetComponent(FPSPlayer); col.GetComponent("HealthScript").health -= 10; if (player) { player.ApplyDamage(10); } }

function Reset () { if (collider == null); gameObject.AddComponent(BoxCollider); collider.isTrigger = true; }

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 landon912 · Aug 31, 2012 at 03:49 AM 1
Share

Please reformat this question: select the script parts and hit box with numbers in it.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Devon Hall · Aug 31, 2012 at 04:42 AM

Ok so from what I could understand, you are calling

 function OnTriggerEnter(col:Collider){}

and you are using a completely different function to attempt to "`Reset()`" something. The conflict here is that programming doesn't exactly work the way you might think. When you have the `(col:Collider)` the parameters aren't universal, rather private and only shared within that function. So to call the local variable "col" when its not even localized in that function won't work. To be a bit more specific. Here is an example.:

You tell a secret to a group of friends that we will call "Group A" and the secret with be called "Secret A". So if you go over to another group of friends ("Group B") and ask them about Secret A. They won't have a single clue about what you are talking about. But if you tell that group the secret then that group will understand.

So the quick fix would be to make the collider variable public within the script and not just local within the function. Since the parameters for your function are required to know them, you can define a variable outside the function and set the variable to the collider inside the function. Example: var collider;

 function OnTriggerEnter(col: Collider){
  collider = col;
 }

Hope I helped.

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

Answer by aldonaletto · Aug 31, 2012 at 12:50 PM

You should place the death checking code inside ApplyDamage, in the FPSPlayer. You're also applying the damage to different variables located in FPSPlayer and HealthScript - don't do that! The ideal approach would be to read the FPSPlayer health variable in HealthScript, instead of having two variables - but in order to not modify your scripts too much, you could simply update the HealthScript health variable in Update, in the FPSPlayer - but remember to remove the duplicated damage code from OnTriggerEnter!
All in all, the OnTriggerEnter code would be like this:

function OnTriggerEnter (col : Collider) {
  var player : FPSPlayer = col.GetComponent(FPSPlayer);
  if (player) {
    player.ApplyDamage(10);
  }
}
I don't know how your FPSPlayer script is implemented, but suppose that it has a health variable, and that the function ApplyDamage just subtracts the damage from it. If it's true, the FPSPlayer script should become something like this:

var health: float; private var hScript: HealthScript;

function Start(){ hScript = GetComponent(HealthScript); // get a reference to HealthScript }

function ApplyDamage(damage: float){ health -= damage; // apply the damage... if (health <= 0){ // and check if player has died // player is dead - place your death code here } }

function Update(){ // update HealthScript.health every frame: hScript.health = health; } NOTE: Take the code above as a suggestion! You must adapt it to your actual FPSPlayer code. If you can't do that, post the script in your question.

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

10 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

Related Questions

Expecting ) found = 1 Answer

How to create an NPC enemy which can take damage and be killed when its energy or health reaches zero? 2 Answers

how do i make a script that calculate how much damage did i loss 1 Answer

For loop on collision (two hit kill) 2 Answers

Falling Damage 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