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);
}
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
Your answer
Follow this Question
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