Hi,
I am using a coroutine to add +1 to my games score as when I do it simply through onCollisionEnter it seems to collide several times with the object and therefore adds more than one integer to the score count. The easiest was I assumed to add it through a coroutine would be using a boolean, however, it never seems to set back to true within the IEnumerator… Where am going wrong? (script below)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class score : MonoBehaviour {
public Text MyText;
private int score;
public bool scoreUp = true;
private IEnumerator coroutine;
void Start () {
MyText.text = "";
coroutine = WaitAndPrint(2.0f);
}
void Update () {
MyText.text = "score:" + score;
}
void OnCollisionEnter (Collision col)
{
if (col.gameObject.name == "Enemy") {
StartCoroutine(coroutine);
print("Starting " + Time.time);
scoreUp = false;
}
}
private IEnumerator WaitAndPrint(float waitTime)
{
yield return new WaitForSeconds (waitTime);
print ("WaitAndPrint " + Time.time);
scoreUp = true;
score = score + 1;
Debug.Log ("bool:" + scoreUp);
}
}