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 /
  • Help Room /
avatar image
0
Question by Fyurien · Oct 26, 2015 at 04:41 AM · c#waitforseconds

C# waitForSeconds Question

Hello,

Just looking to get some help and possibly have someone point me in the right direction. What I'm trying to achieve with this script is to increment the score by 1 point every second. The goal is to have it show on screen as increasing from 0, 1, 2, 3, 4. Instead of going from 0 to 4 directly. Kind of like it visually counts up one number at a time.

So I am using the waitForSeconds function, but no luck. It still goes from 0 to 2 instantly. Debug shows everything happening at once, so no waiting. Here is the script...

 using UnityEngine;
 using System.Collections;
 
 public class increaseScore : MonoBehaviour
 {
     private int counter = 0;
     public GUIStyle customGuiStyle;
 
     void Start()
     {
         
     }
     
     void Update()
     {
         //counter++;    
     }
 
     void  OnTriggerEnter ( Collider other  )
     {
         
         if (other.gameObject.tag == "Gold")
         {
             counter+=1;
             StartCoroutine(Wait());
             Debug.Log("1 second is up");
 
             counter+=1;
             StartCoroutine(Wait());
             Debug.Log("2 second is up");
         }
 
         /*
         else if (other.gameObject.tag == "Diamond")
         {
             counter+=75;    
         }
         */
         
     }
 
 
     void OnGUI()
     { 
 
         int x = 100;
         int y = 50;
         int w = 50;
         int h = 50;
         GUI.Label(new Rect(x, y, w, h), "Score  " + counter, customGuiStyle);
     }
 
 
     IEnumerator Wait()
     {
         Debug.Log("waiting");
         yield return new WaitForSeconds(5f);      
     }
 }

Update:

Well I figured it out. It aint pretty but this works.

 using UnityEngine;
 using System.Collections;
 
 public class increaseScore : MonoBehaviour
 {
     private int counter = 0;
     public GUIStyle customGuiStyle;
 
     void Start()
     {
         
     }
     
     void Update()
     {
         //counter++;    
     }
 
     void  OnTriggerEnter ( Collider other  )
     {
         
         if (other.gameObject.tag == "Gold")
         {
 
             StartCoroutine(GoldWait());
 
         }
 
     
         else if (other.gameObject.tag == "Diamond")
         {
             StartCoroutine(DiamondWait());
         }
 
         
     }
 
 
 
     void OnGUI()
     { 
 
         int x = 100;
         int y = 50;
         int w = 50;
         int h = 50;
         GUI.Label(new Rect(x, y, w, h), "Score  " + counter, customGuiStyle);
     }
 
 
     IEnumerator GoldWait()
     {
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
     }
 
     IEnumerator DiamondWait()
     {
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
         counter+=1;
         yield return new WaitForSeconds(0.05f); 
     }
     
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
0
Best Answer

Answer by Leoo · Oct 26, 2015 at 05:48 AM

OMG HLL NO! , <- Sorry about that.. i had to when i saw your code.

Hey @Fyurien , well lets get that code clean, even that gui , something like this?.

         //Define" gui" variables outside OnGui function! Then use these..
          int x = 100;
          int e = 50;

         //Score Per Diamond
         public int scoreDiamond = 35;   
         //Score per Gold
         public int scoreGold = 10;           
         
         //Total Score
         int counter = 0;  
         //Pool how many points left to add to the counter (safer if multiple triggers get called at the same time)     
         int addScore = 0;     
 
         //Save some ram bytes by caching the waitforseconds.
         WaitForSeconds waitPointFive = new WaitForSeconds(0.05f);
     
         void OnTriggerEnter(Collider other)
         {
             if (other.gameObject.tag == "Gold")
             {
                 StartCoroutine(increaseScore(scoreGold));
             }
             else if (other.gameObject.tag == "Diamond")
             {
                 StartCoroutine(increaseScore(scoreDiamond));
             }
         }
     
     
         IEnumerator increaseScore(int newScore)
         {
             addScore += newScore;
             while (addScore != 0)
             {
                 addScore--;
                 counter++;
                 yield return waitPointFive;
             }
         }
 

I hope that helps you out! , -Leo

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 Fyurien · Nov 01, 2015 at 02:47 PM 1
Share

Thanks bud, I really appreciate it! :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

I want my script to wait 2 seconds before continue in a condition, in update, using C# 2 Answers

How to wait a certain amount of seconds in C# 4 Answers

How to make a function wait (**I have looked elseware and havent found a well explained solution**) 0 Answers

Wait time after coroutine's wait seconds is complete 0 Answers

WaitForSeconds don't work with variables from another scripts 2 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