yield WaitForSeconds unexpected symbol
Hello All. I'm currently adding some functionality to the first "Roll A Ball" application as I learn Unity for the first time. I wanted to display the Win text for X number of seconds so I found the yield WaitForSeconds function. The issue is that the compiler in MonoDevelop-Unity application gives me an error:
Assets/Scripts/PlayerController.cs(13,17): error CS0246: The type or namespace name `List`1' could not be found. Are you missing a using directive or an assembly reference?
All I did was add this to the "SetCountText()" function as below:
void SetCountText(){
countText.text = "Count: " + count.ToString ();
if (count >= 17) {
winText.text = "This game is terrible. You should get a refund.";
yield WaitForSeconds(5f);
}
}
Any ideas? Am I missing some special namespace or something? I'm currently using: using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI;
Can I bump this? Still need a good answer as this still doesn't seem to work even done as described in the suggested links.
Answer by FortisVenaliter · May 25, 2016 at 04:07 PM
WaitForSeconds is a class, so it needs to be instantiated with the 'new' keyword. Try this:
yield return new WaitForSeconds(5f);
Also, it only works in a function that supports it, namely a function that returns IEnumerator. Check this article out for more info.
using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI;
public class PlayerController : $$anonymous$$onoBehaviour {
public float speed;
public Text countText;
public Text winText;
private int count;
private List<GameObject> disabledObjects = new List<GameObject>();
private Rigidbody rb;
void Start(){
rb = GetComponent<Rigidbody> ();
count = 0;
SetCountText ();
winText.text = "";
StartCoroutine (checkEnd());
}
// Physics code. Called before physics calculations
void FixedUpdate(){
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement*speed);
}
void Update(){
if (count >= 17) {
// reset game
count = 0;
rb.$$anonymous$$ovePosition(new Vector3 (0.0f, 0.5f, 0.0f));
SetCountText ();
foreach (GameObject a in disabledObjects) {
a.SetActive (true);
}
}
}
void OnTriggerEnter(Collider other){
if (other.gameObject.CompareTag("Pickup")){
other.gameObject.SetActive(false);
disabledObjects.Add (other.gameObject);
count++;
SetCountText ();
}
}
void SetCountText(){
countText.text = "Count: " + count.ToString ();
}
IEnumerator checkEnd(){
if (count >= 17) {
winText.text = "This game is terrible. You should get a refund.";
yield return new WaitForSeconds (5.0f);
winText.text = "";
}
}
}
Did you read the article I linked to? It tells you how to start coroutines properly. Hint: It's how you're calling the method.
Yep. I tried calling checkEnd(), StartCoroutine(checkEnd()) as well as StartCoroutine("checkEnd"); - Like in updated code in previous comment (I Edited it). And it still doesn't seem to work.
Answer by alpsaruhan96 · Nov 25, 2020 at 11:09 PM
hey, none of this worked? well, try this one...
IEnumerator WaitForSec(float secounds)
{
Debug.Log("our wait has started");
yield return new WaitForSeconds(secounds);
Debug.Log("our wait has started");
}
Your answer
Follow this Question
Related Questions
Unity do not open the Creator Kit: Beginner code in the right version. 0 Answers
Unity hub help 0 Answers
Ar foundation Face Tracking 0 Answers
how to download open jdk,sdk, ndk recommended for unity Unity 2019.3.1, without unity hub. 1 Answer
I cant get the basic FollowPlayer script to go to Main Camera in the hierarchy 0 Answers