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 /
This question was closed Jan 24, 2015 at 07:15 PM by Ujean917 for the following reason:

Other

avatar image
0
Question by Ujean917 · Jan 24, 2015 at 02:30 AM · c#timer

Trying to Fill Up a Bar Slowly but Surely

Hello. I am trying to make a game where the player can utilize an augmented attack that they can use from time to time. They must wait for however much time to pass in order for this bar to fill up completely. Once the bar is completely filled to 100 percent it is then the player is allowed to use this super attack. However I am having trouble over how to scale this bar in its localScale without crashing my unity client (I think I might've caused an infinite loop in some way or form without realizing it and it only happened recently when trying to implement this scaling bar).

Here is the code that I am trying to make this happen but failed to do so:

     using UnityEngine;
     using System.Collections;
     
     public class AlternateAttackProtocol : MonoBehaviour 
     {
         private static AlternateAttackProtocol instance = null;
         public static AlternateAttackProtocol Instance
         {
             get {return instance; }
         }
     
         public Transform alternateAttackDisplay;
         public float maxAlternateAttack = 100.0f;
         public float currentAlternateAttack;
         private float alternateAttackOriginalXScale;
     
 
         void Start () 
         {
             alternateAttackOriginalXScale = alternateAttackDisplay.localScale.x;
             currentAlternateAttack = 0.0f;
         }
         
         // Update is called once per frame
         void Update () 
         {
 
    //This body of code right here I am trying to make this bar scale accordingly over time.
 
                 currentAlternateAttack = currentAlternateAttack + 1; 
         
                 AlternateAttackTimer();
         
                 AlternateAttackInitiated ();
             }
         
             void AlternateAttackTimer()
             {
                 while (currentAlternateAttack <= maxAlternateAttack)
                 {
                     alternateAttackDisplay.localScale = new Vector3(alternateAttackOriginalXScale * (currentAlternateAttack/maxAlternateAttack), alternateAttackDisplay.localScale.z, alternateAttackDisplay.localScale.z);
                 }
             }
         
             void AlternateAttackInitiated()
             {
                 if (currentAlternateAttack == 100 && Input.GetKeyDown ("Fire2")) 
                 {
                     currentAlternateAttack = 0.0f;
                 }
             }
         }



If there is some better way to do this such as Time.deltaTime please advise. I tried doing that as well but that caused its own way of crashing the client which I'm afraid of doing. Thank you in advance.

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

  • Sort: 
avatar image
0
Best Answer

Answer by getyour411 · Jan 24, 2015 at 07:20 AM

You might want to go in a different direction; look at the Unity 4.6 new GUI tutorial on UI sliders which also work very well for bars like Health, Mana, Fuel, or Power-Up; an example script interface is shown in the vid towards the end.

http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-slider

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 Ujean917 · Jan 24, 2015 at 05:38 PM 0
Share

Thanks for suggesting that but the UI design tutorial didn't seem to have a 3-D look the way I wanted it and were just flat images that changed lengths when you slide the values. Here's an image for an example of what $$anonymous$$e looks like. alt text

I always tended to like things in 3D however when I do get the chance of trying new things I might want to do that in the later future.

UPDATE: Figured out I could do without the while loop since it was already being called in the update method. All I needed was to replace the while loop with an if statement that only works while it is under or equal to 100. Thanks for trying to help however..

untitled2.png (2.8 kB)

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a fill take exactly n seconds to complete 2 Answers

How to integrate timer into my C# script? 2 Answers

How to create a delay between spawning of tiles. 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