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
1
Question by SUDGaming · Feb 23, 2013 at 06:38 AM · prefabplayerhealth

How to make my Player take damage from prefab?

 var curHealth = 100;
 var maxHealth = 100;
 var healthtext : GUIText;
 var HitDamage : boolean;
 var deathbomb : GameObject;
 
 
 function OnControllerColliderHit(hit: ControllerColliderHit){
 
     if(hit.gameObject.name == "Enemy");
     {
         HitDamage == true;
         curHealth -= 10;
     }
     
 function Update(){
     healthtext.text = "Health " + curHealth + " / " + maxHealth;
         if(curHealth <=0){
             Destroy(gameObject);
             }
     }
 }
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
2
Best Answer

Answer by AlucardJay · Feb 25, 2013 at 05:51 AM

There are many many syntax errors in your script. Read your script like the computer would

line 10 :

 if(hit.gameObject.name == "Enemy");

your semicolon closes the if statement, so nothing happens after that, and lines 11-15 would be considered an error. No semicolon after the conditional of an if statement.

line 12 :

 HitDamage == true;

== is used to check if something is equal to, to make something equal something else, just use one equals sign.

line 17 :

 healthtext.text = "Health " + curHealth + " / " + maxHealth;

UI don't use Unity GUI so am not sure if you can pass an int to a string this way, I assume not. TO convert something to a string, use .ToString()

Curly Braces :

Your ControllerCollider function doesn't have the closing curly braces until after your Update function. Make sure each function is enclosed between {}

Declaring variables :

 var curHealth = 100;

this is a personal thing as unity can fix this for you when compiling, but you should really typecast all your variables. That also proves you know what a variable is and how it can be interacted with. use

 var curHealth : float = 100; or var curHealth : int = 100;


So I have editedyour script with these considerations. Please take the time to read it and yours, discover the differences and learn from it.

 var curHealth : int = 100;
 var maxHealth : int = 100;
 var healthtext : GUIText;
 var HitDamage : boolean;
 var deathbomb : GameObject;
 
 
 function OnControllerColliderHit( hit: ControllerColliderHit )
 {
     Debug.Log( "Controller hit : " + hit.gameObject.name ); // what did the controller collide with?
     
     if ( hit.gameObject.name == "Enemy" ) // if is equal to
     {
         HitDamage = true; // equals
         curHealth -= 10;
     }
 }
     
 function Update()
 {
     healthtext.text = "Health " + curHealth.ToString() + " / " + maxHealth.ToString(); // ToString()
     
     if ( curHealth <= 0 )
     {
         Destroy( gameObject );
     }
 }
Comment
Add comment · Show 1 · 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 AlucardJay · Mar 07, 2013 at 11:23 AM 1
Share

Hello. If this answer helped, could you please mark it as accepted. Thanks.

On the left-hand-side of the Answer box , there are the following icons :

 Thumb Up
 Number (of votes)
 Thumb Down 
 A Tick/Check $$anonymous$$ark

If an answer worked for you, click on the 'Tick/Check mark', the answer should now be highlighted in green and marked as accepted.

http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

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

Player Respawn After Death 1 Answer

Problems with a player spawning and handling script 1 Answer

I want to attack me enemy and decrease their health, but t won't work, what am I doing wrong with my script? 4 Answers

How to keep health from going over 100% when heath pickup is used and why does the item effect stop working after I instantiate it from the vendor. 2 Answers

Enemy 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