• 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
Question by jonathanbooker2911 · Nov 29, 2018 at 07:59 AM · scripting problemscript.scripting beginnerscriptingbasicsscriptingproblem

Trying to find the highest number than add it to itself.

using System.Collections; using System.Collections.Generic; using UnityEngine;

[System.Serializable] public class HumanRacial : MonoBehaviour { public float agility; public float strength; public float intellect; public float highestStat; public float coolDown = 120; public float coolDownTimer; public float duration; public float durationTimer;

 public void Start()
 {
     
 }

 public void Update()
 {          
     Active();
     Cooldown();
 }

 public void RacialBonus()
 {
     float maxValue = Mathf.Max(agility, strength, intellect);

     if ((agility == maxValue && (agility == strength || agility == intellect)) || (strength == maxValue && strength == intellect))
     {
         //Two or more of our attributes are equally maxValue
         //Decide how to deal with this situation

     }
     else if (agility == maxValue)
     {
         agility *= 2f;
     }
     else if (strength == maxValue)
     {
         strength *= 2f;
     }
     else if (intellect == maxValue)
     {
         intellect *= 2f;
     }
 }

 private void Cooldown()
 {

     if (duration > 0)
     {
         durationTimer -= Time.deltaTime;
     }
     if (durationTimer < 0)
     {
         durationTimer = 0;
     }

     if (coolDownTimer > 0)
     {
         coolDownTimer -= Time.deltaTime;
     }
     if (coolDownTimer < 0)
     {
         coolDownTimer = 0;
     }
 }

 private void Active()
 {
     if (Input.GetKeyDown(KeyCode.D) && coolDownTimer == 0 && durationTimer == 0)
     {
         coolDownTimer = coolDown;
         durationTimer = duration;
         RacialBonus();
     }
 }

}

Comment

People who like this

0 Show 3
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 ShadyProductions · Nov 29, 2018 at 08:02 AM 1
Share

Maybe you should explain your problem a bit instead of just posting a bunch of code and a title which doesn't give a lot of information about the actual problem you are having?

avatar image ecv80 · Nov 29, 2018 at 09:06 AM 0
Share

What @ShadyProductions said. As a rule of thumb when asking questions, one should at the very least explain what the problem is or what is currently happening, all the better the more specific one is, giving error codes and the lines where these point to, or the problematic variable or methods, AND what one intends to achieve, the expected behavior, being as specific as possible too. That's if one expects to get any or good answers at all. Failing to do so will probably yield misleading or no answers, because most of us can't read minds nor have crystal balls.

avatar image jonathanbooker2911 ecv80 · Nov 29, 2018 at 10:19 AM 0
Share

I thought the title was enough I was trying to find the highest value out of all three numbers. Than adding it to one of the previous values. I did it with if statements. I want to know if they was a more efficent way to do it. public void Start() {

} public void Update() {
Active(); if (duration > 0) { durationTimer -= Time.deltaTime; } if (durationTimer < 0) { durationTimer = 0; } if(coolDownTimer > 0) { coolDownTimer -= Time.deltaTime; } if(coolDownTimer < 0) { coolDownTimer = 0; } } public void RacialBonus() { highestStat = Mathf.Max(agility, strength, intellect); float myHighestStat = Mathf.Max(agility, strength, intellect); highestStat += myHighestStat; if((agility > intellect) && (agility > strength)) { agility += highestStat; } if ((strength > intellect) && (agility > strength)) { strength += highestStat; } if ((intellect > agility) && (intellect > strength)) { intellect += highestStat; } } private void Active() { if (Input.GetKeyDown(KeyCode.D) && coolDownTimer == 0 && durationTimer == 0) { coolDownTimer = coolDown; durationTimer = duration; RacialBonus(); } }

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by calpolican · Nov 29, 2018 at 08:06 AM

You're overwriting the value. Just store it in a temporary variable.

And try to use lower case for variables.

  float myHighestStat= Mathf.Max(Agility, Strength, Intellect);
  HighestStat += myHighestStat;   





Comment
ecv80

People who like this

1 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 jonathanbooker2911 · Nov 29, 2018 at 08:50 AM 0
Share

using System.Collections; using System.Collections.Generic; using UnityEngine;

[System.Serializable] public class HumanRacial : MonoBehaviour { public float agility; public float strength; public float intellect; public float highestStat; public float coolDown = 120; public float coolDownTimer; public float duration; public float durationTimer;

 public void Start()
 {
     
 }

 public void Update()
 {          
     Active();


     if (duration > 0)
     {
         durationTimer -= Time.deltaTime;
     }
     if (durationTimer < 0)
     {
         durationTimer = 0;
     }

     if(coolDownTimer > 0)
     {
         coolDownTimer -= Time.deltaTime;
     }
     if(coolDownTimer < 0)
     {
         coolDownTimer = 0;
     }
 }

 public void RacialBonus()
 {
     highestStat = Mathf.Max(agility, strength, intellect);
     float myHighestStat = Mathf.Max(agility, strength, intellect);
     highestStat += myHighestStat;

     if((agility > intellect) && (agility > strength))
     {
         agility += highestStat;
     }
     if ((strength > intellect) && (agility > strength))
     {
         strength += highestStat;
     }
     if ((intellect > agility) && (intellect > strength))
     {
         intellect += highestStat;
     }
 }

 private void Active()
 {
     if (Input.GetKeyDown(KeyCode.D) && coolDownTimer == 0 && durationTimer == 0)
     {
         coolDownTimer = coolDown;
         durationTimer = duration;
         RacialBonus();
     }
 }

}

I tried your changes but the only way I know hot to fix this is with if statements.

avatar image

Answer by ecv80 · Nov 29, 2018 at 01:04 PM

Okay, so if I got you right, you want to double whatever the highest attribute is. There really isn't much room for efficiency improvement from what you have already done in your last code. Another way to put it would be:

 float maxValue=Mathf.Max(agility, strength, intellect);
         
         if ( (agility==maxValue && (agility==strength || agility==intellect)) || (strength==maxValue && strength==intellect)) {
             //Two or more of our attributes are equally maxValue
             //Decide how to deal with this situation
 
         }
         else if (agility==maxValue) {
             agility*=2f;
         }
         else if (strength==maxValue) {
             strength*=2f;
         }
         else if (intellect==maxValue) {
             intellect*=2f;
         }

You might find this clearer to work with... or not... that's entirely your preference.

EDIT: But all in all, @calpolican 's answer should be the valid answer to the original question. Again, if I got all this right.

Comment

People who like this

0 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 ecv80 · Nov 29, 2018 at 01:09 PM 0
Share

Tell me if I missed the point. Also, I don't wanna be a pest but I had to copy your code from your reply comment to my comment in your question and format it manually only to realize it's the same code you posted as an answer, so please use code tags. It's also a good idea to edit your original question to reflect changes along the way.

avatar image jonathanbooker2911 · Nov 29, 2018 at 07:04 PM 0
Share

True but now how do I make the bonus stop when the duration ends?

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

182 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 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 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 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 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 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 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 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 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

How do I make do I make a variable go back to its original value for a spell system 1 Answer

Point Counter Works Only Once! 1 Answer

how to allow the key to only open 1 door rather than all of them? 0 Answers

Help me with this code proplem,,Almost finish my game please help this once 3 Answers

Why when using get; set; in one script i'm getting null in other script that use it ? 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