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
-3
Question by ChocopopGames · Jan 07, 2014 at 06:48 PM · javascripterrorrpg-gamehealth

How can i solve this error?because i am doing the right things

 #pragma strict
 var health : float = 100.0;
 var hunger : float = 100.0;
 
 function Start () {
 
 }
 
 function Update () {
 
 hunger -= Time.deltaTime/3;
 
 if (hunger <=5){
     
     health --;
 
 }
 
 if (health <=0){
 
     print ("Moriste!");
     
     }
  
  health = Mathf.(lamp(health, 0.0, 100.0);   
  hunger = Mathf.(lamp(hunger, 0.0, 100.0);   
          
 }
 
 function OntriggerEnter (other : Collider) {
 
 if (other.gameObject.name == ("Banana")&& hunger <=90.0){
 
 health += 10;
 Destroy (other.gameObject.transform.parent.gameObject);
 
    }
 
 }
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 CHPedersen · Jan 07, 2014 at 07:11 PM 0
Share

[Edit: Hit the code format button for him, and removed the "java" tag]

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Datates · Jan 07, 2014 at 07:52 PM

There are a few problems with the script:

 health = Mathf.(lamp(health, 0.0, 100.0);
 hunger = Mathf.(lamp(hunger, 0.0, 100.0); 

Replace (lamp with Clamp..

 health = Mathf.Clamp(health, 0.0, 100.0);
 hunger = Mathf.Clamp(hunger, 0.0, 100.0); 

Next one is the function name, it is case sensitive:

 function OntriggerEnter (other : Collider)

should be:

 function OnTriggerEnter (other : Collider)

And third:

 Destroy(other.gameObject.transform.parent.gameObject);

This is not really an error, but it included an unnecessary step

 Destroy(other.transform.parent.gameObject);

There's another problem though, which you should fix yourself: your health will get reduced to 0 very fast because of this little piece:

 if (hunger <= 5)
 {
     health--;
 }

Look at what you did with hunger, and do the same to health (hint: deltaTime)


The end result:

 #pragma strict
 
 var health : float = 100.0;
 var hunger : float = 100.0;
 
 function Start()
 {
 
 }
 
 function Update()
 {
     hunger -= Time.deltaTime * 0.33;
 
     if (hunger <= 5)
     {
         health--;
     }
 
     if (health <= 0)
     {
         print("Moriste!");
     }
 
     health = Mathf.Clamp(health, 0.0, 100.0);
     hunger = Mathf.Clamp(hunger, 0.0, 100.0);
 }
 
 function OnTriggerEnter(other : Collider)
 {
     if (other.gameObject.name == "Banana" && hunger <= 90.0)
     {
         health += 10;
         Destroy(other.transform.parent.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 Gruffy · Jan 07, 2014 at 08:34 PM 0
Share

well done matey :)

avatar image
0

Answer by Gruffy · Jan 07, 2014 at 07:39 PM

trying adding the "C" to your mathf.lamp statements.. you have ...

      health = Mathf.(lamp(health, 0.0, 100.0);
     hunger = Mathf.(lamp(hunger, 0.0, 100.0); 

when you should have...

  health = Mathf.Clamp(health, 0.0, 100.0);
 hunger = Mathf.Clamp(hunger, 0.0, 100.0); 


hope that helps bud

take care Gruffy

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 ChocopopGames · Jan 07, 2014 at 10:00 PM 0
Share

Thank you guy you are great, sorry for that stupid question but im very noob with unity yet ;)

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

Health not counting down? 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Error BCE0044 plz help 0 Answers

How to use if statement with var? 3 Answers

Animation Crossfade won't play but Animation Play does 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