Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Tubestorm · May 15, 2020 at 04:15 PM · uisliderupdate functionupdate problem

UI Slider looses control in Update function

Hello!

i'm having some issues with my UI slider that displays the energy of a character.


when an item is bought, I want the energy to move up only the value of the bought item. but it just fills it up to the end. I believe this is because of how Update is called once every frame and the button press might take longer than that to receive information and add the amount indicated. (i just don't know how to fix this)


essentially is there a better way to not have my code optimized so it doesn't do this.

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using UnityEngine.UI;
     
     public class CharacterStats : MonoBehaviour
     {
         public static CharacterStats instace;
         static protected int happinessValue = 100;
         static protected float energyValue = 100;
         static protected int smartValue = 100;
         static protected int looksValue = 100;
         static protected int money = 500;

         [Header("Character UI")]
         [SerializeField] private Slider happiness;
         [SerializeField] private Slider energy;
         [SerializeField] private Slider smart;
         [SerializeField] private Slider looks;
         [SerializeField] private GameObject moneyUI;
         public int count = 0;
     
         [HideInInspector]
         private Vector3 last_pos;
         private float damage;
     
     
     
     
     
         void Start()
         {
             instance = this;
     
             //----------------LOAD DATA-----------------
     
             happiness.value = 45;
             energy.value = energyValue;
             smart.value = smartValue;
             looks.value = looksValue;
     
             last_pos = transform.position;
             damage = 5;
     
             //---------------------------------------------
     
         }
     
     
         void Update()
         {
             moneyUI.GetComponent<Text>().text = "" + money;
             isWalking();

       //this is where the purchase function is called
             purchase();
     
             
     
         }
     
     
     
         void isWalking()
         {
     
             if (WorldInteraction.instance.velocity1[0] != 0 || WorldInteraction.instance.velocity1[1] != 0)
             {
     
                 energy.value -= damage * (Vector3.Distance(transform.position , last_pos)) * Time.fixedDeltaTime;
                 happiness.value += (damage - 1) * (Vector3.Distance(transform.position, last_pos)) * Time.fixedDeltaTime;
                 last_pos = transform.position;
             }
             else
             {
                 Debug.Log("not moving");
     
             }
         }
     
     
     
 //here is the function that collects the amount of energy to add based on what is bought
         public void purchase()
         {
             energy.value += ShopItemSearch.GetEnergy();
             happiness.value += ShopItemSearch.GetEnergy() * -1;
     
             Debug.Log("purchase" + ShopItemSearch.GetEnergy());
         }
     
     
     
     /*------------------------------------------------------------------------------------
     //---------------------------------SAVING SCENE DATA-----------------------------------
     /------------------------------------------------------------------------------------*/
     
         void OnDestroy()
         {
         }
     
         /*---------------------------STATS GETTERS AND SETTERS---------------------------*/
     
         public static int GetMoney()
         {
             return money;
         }
     
         public static void SetMoney(int price)
         {
             money = price;
         }
     
         public static int GetHappiness()
         {
             return happinessValue;
         }
     
         public static void SetHappiness(int value)
         {
             happinessValue += value;
         }
     
         public static float GetEnergy()
         {
             return energyValue;
         }
     
         public static void SetEnergy(float value)
         {
             energyValue += value;
         }
     
     
     }
     






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
1
Best Answer

Answer by diwang · May 15, 2020 at 05:12 PM

If i wasn't wrong , this is your purchase()

 energy.value += ShopItemSearch.GetEnergy();
 happiness.value += ShopItemSearch.GetEnergy() * -1;

And this is your GetEnergy()

 public static float GetEnergy()
          {
              return energyValue;
          }

So it equals to this

     energy.value += energyValue;
     happiness.value += energyValue * -1;

And i see nowhere in your code that energyValue is reduced in any mean , so therefore the energy.value will keep rising because energyValue is never reduced .
Solution : set energyValue to zero after using it

 energy.value += ShopItemSearch.GetEnergy();
 happiness.value += ShopItemSearch.GetEnergy() * -1;
 energyValue = 0;

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 Tubestorm · May 16, 2020 at 01:18 PM 0
Share

Hello! this really helped me out thanks.

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

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

Dynamic slider size with the new UI 2 Answers

left end of slider fill flattening the more i move the value down 0 Answers

Why hip will move upward and downward automatically when start to drag slider at x,y and z axis? 0 Answers

Rotatin the Camera using the UI slider value 1 Answer

How to put a image under a powerbar's handle thats moving? 0 Answers


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