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);
}