• 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 Shears · Aug 03, 2015 at 12:56 PM · variableupdatefunctiondamage

Variable Doesn't Update in "Update" function

Ive been working on fixing this code for a day and a half now and can not figure it out. I've googled and googled but cant find a decent answer.

Im trying to work with this code... (Javascript)

 #pragma strict
 
 static var playerDamage : int;
 var Distance : float;
 var MaxDistance : float = 1.5;
 
 static public var TheDamage : int = 2;
 
 function Update()
 {
     if(Input.GetButtonDown("Fire1")) 
     {
         if ((MeleeLevel.MeleeLevel == 1) && (MeleeLevel.MeleeLevel < 5))
         {
             playerDamage = 2;
         }
         if ((MeleeLevel.MeleeLevel >= 5) && (MeleeLevel.MeleeLevel < 10))
         {
             playerDamage = 3;
         }
         if ((MeleeLevel.MeleeLevel >= 10) && (MeleeLevel.MeleeLevel < 20))
         {
             playerDamage = 4;
         }
         if ((MeleeLevel.MeleeLevel >= 20) && (MeleeLevel.MeleeLevel < 30))
         {
             playerDamage = 5;
         }
         if ((MeleeLevel.MeleeLevel >= 30) && (MeleeLevel.MeleeLevel < 40))
         {
             playerDamage = 7;
         }
         if ((MeleeLevel.MeleeLevel >= 40) && (MeleeLevel.MeleeLevel < 50))
         {
             playerDamage = 9;
         }
         if ((MeleeLevel.MeleeLevel >= 50) && (MeleeLevel.MeleeLevel < 60))
         {
             playerDamage = 12;
         }
         if ((MeleeLevel.MeleeLevel >= 60) && (MeleeLevel.MeleeLevel < 70))
         {
             playerDamage = 15;
         }
         if ((MeleeLevel.MeleeLevel >= 70) && (MeleeLevel.MeleeLevel < 80))
         {
             playerDamage = 20;
         }
         if ((MeleeLevel.MeleeLevel >= 80) && (MeleeLevel.MeleeLevel < 90))
         {
             playerDamage = 25;
         }
         if ((MeleeLevel.MeleeLevel >= 90) && (MeleeLevel.MeleeLevel < 100))
         {
             playerDamage = 31;
         }
         if (MeleeLevel.MeleeLevel == 100)
         {
             playerDamage = 37;
         }
         TheDamage = playerDamage * Random.Range(0,10);
     }
         if(Input.GetButtonDown("attack02")) 
         {
         if ((MeleeLevel.MeleeLevel == 1) && (MeleeLevel.MeleeLevel < 5))
         {
             playerDamage = 2;
         }
         if ((MeleeLevel.MeleeLevel >= 5) && (MeleeLevel.MeleeLevel < 10))
         {
             playerDamage = 3;
         }
         if ((MeleeLevel.MeleeLevel >= 10) && (MeleeLevel.MeleeLevel < 20))
         {
             playerDamage = 4;
         }
         if ((MeleeLevel.MeleeLevel >= 20) && (MeleeLevel.MeleeLevel < 30))
         {
             playerDamage = 5;
         }
         if ((MeleeLevel.MeleeLevel >= 30) && (MeleeLevel.MeleeLevel < 40))
         {
             playerDamage = 7;
         }
         if ((MeleeLevel.MeleeLevel >= 40) && (MeleeLevel.MeleeLevel < 50))
         {
             playerDamage = 9;
         }
         if ((MeleeLevel.MeleeLevel >= 50) && (MeleeLevel.MeleeLevel < 60))
         {
             playerDamage = 12;
         }
         if ((MeleeLevel.MeleeLevel >= 60) && (MeleeLevel.MeleeLevel < 70))
         {
             playerDamage = 15;
         }
         if ((MeleeLevel.MeleeLevel >= 70) && (MeleeLevel.MeleeLevel < 80))
         {
             playerDamage = 20;
         }
         if ((MeleeLevel.MeleeLevel >= 80) && (MeleeLevel.MeleeLevel < 90))
         {
             playerDamage = 25;
         }
         if ((MeleeLevel.MeleeLevel >= 90) && (MeleeLevel.MeleeLevel < 100))
         {
             playerDamage = 31;
         }
         if (MeleeLevel.MeleeLevel == 100)
         {
             playerDamage = 37;        
         }
         
         TheDamage = playerDamage * Random.Range(0,10) * 1.45;
     }
 }
 
 function PlayerDamage ()
 {
     var hit : RaycastHit;
     if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
     {
         Distance = hit.distance;
         if (Distance < MaxDistance)
         {
             hit.transform.SendMessage("ApplyDammage", TheDamage, SendMessageOptions.DontRequireReceiver);
         }
     }
     Debug.Log("Player Damage " + playerDamage + " Damage with random " + TheDamage);
 }
 

The code takes my "Melee Level" and adds a damage value to playerDamage depending on my Melee Level. Then takes playerDamage * a random number to = TheDamage(Var)

but the PlayerDamage() function will not read the new TheDamage in the Update() function, it will always read it as "2" where I created the variable at the top.

so everytime I hit the enemy the damage dealt is always 2.

I hope my explanation is understandable.. any help is appreciated. thanks!

Comment

People who like this

0 Show 2
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 DiegoSLTS · Aug 03, 2015 at 02:06 PM 0
Share

Are you sure "Fire1" and "attack02" buttons are correctly configured? I mean, do those "if" evaluate to true?

Other than that, even if your MeeleLevel is not in the [1,100] range the variable TheDamage should change.

Add a "Debug.Log(TheDamage);" line after the lines where you change the TheDamage value to make sure the code at that point was executed.

avatar image NoseKills · Aug 03, 2015 at 02:06 PM 0
Share

Have you tried debugging? Put Debug.Log calls inside the if-blocks so you see if you ever go into those ifs. You can also print out the MeleeLevel and playerDamage to see where the mistake happens.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to prevent object (bullet) from instantiating twice ? (It instantiates once for tapping the screen, and once more when beginning to hold touch on screen) ? 1 Answer

Update() doesnt call method or modify values 1 Answer

Having code fire once in update function? 2 Answers

summing up items price in runtime 1 Answer

IEnumerator problem 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