- Home /
WaitForSeconds Not Working
I need to delay an Inventory's open time, but "yield return WaitforSeconds" is not working. Here is the coroutine's code:
public IEnumerator CheckIfStillHover() {
yield return new WaitForSecondsRealtime (1.5f); // change to options class variable once it is created
print ("Fine. I admit it. I cut in line. Happy?");
}
In case this has something to do with the way I called it, which I also had difficulty with (if anyone knows why I had to store it in a variable to call it, please tell me), here is the code from a separate script which I used to call the coroutine:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof (InventoryRenderer))]
public class ToggleInventoryHover : MonoBehaviour {
//name the inventory to match this string (or vice-versa)
public string inventoryName = "Inventory";
GameObject inventory;
IEnumerator coroutine;
bool isHover = false;
void Start() {
inventory = GameObject.Find(inventoryName);
coroutine = inventory.GetComponent<InventoryRenderer> ().CheckIfStillHover ();
}
void OnMouseOver() {
isHover = true;
inventory.GetComponent<InventoryRenderer> ().StartCoroutine(coroutine);
}
void OnMouseExit() {
isHover = false;
}
}
You are not starting the coroutine in this code. Please see: https://docs.unity3d.com/$$anonymous$$anual/Coroutines.html
@foureyes44, listen to kaplica's comment or nviverosb's answer, the problem is that you don't use StartCoroutine()
. Just replace your line in Start()
with this:
StartCoroutine(inventory.GetComponent<InventoryRenderer> ().CheckIfStillHover ());
so I get something like this for the first script. I have not tested it. Just edited it in editor but never built.
[RequireComponent (typeof (InventoryRenderer))]
public class ToggleInventoryHover : $$anonymous$$onoBehaviour
{
//name the inventory to match this string (or vice-versa)
public string inventoryName = "Inventory";
public float delay = 1.5; //delay for inventory open in seconds
GameObject inventory;
bool isHover = false;
void Start() {
inventory = GameObject.Find(inventoryName);
}
void On$$anonymous$$ouseOver (){
if (isHover = true)
StartCoroutine (LoadAfterDelay (delay));
IEnumerator LoadAfterDelay(float delay)
yield return new WaitForSeconds (delay);
// Do next directions here
// call to get -> inventory.GetComponent<InventoryRenderer> ().CheckIfStillHover ();
}
void On$$anonymous$$ouseExit() {
isHover = false;
}
}
Then for the second script being called after delay
//2nd script something like this//
public class CheckIfStillHover;
bool isHover = true;
void Start(){
if isHover = true
print ("Fine. I admit it. I cut in line. Happy?")
}
void On$$anonymous$$ouseExit() {
isHover = false;
//
}
Answer by nviverosb · Apr 23, 2018 at 11:25 PM
Here's a possible solution you're not keeping in mind:
WaitForSeconds DOES NOT WORK AS A DELAY OUTSIDE THE COROUTINE.
When you start a Coroutine, it executes itself, but the script inmediately executes the following instruction. For a WaitForSeconds to work, you need to put the code to open the inventory inside the coroutine method.
It would look something like this:
public IEnumerator CheckIfStillHover() {
yield return new WaitForSecondsRealtime (1.5f); // change to options class variable once it is created
//Whatever you want to delay goes in here
print ("Fine. I admit it. I cut in line. Happy?");
}
Answer by Karsten · Apr 22, 2018 at 07:38 PM
i would use the Invoke mehod for this its much more straight forward and in the end is doing the same behind the scenes https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html Also check if OnMouseOver is really called
How is the invoke method doing a coroutine? Invoke, as the name implies, invokes a method, on the main thread of course, couroutin is being ran over multiple frames, which is different to a normal method invoke.
he wants to wait 1,5 seconds, no need for a coroutine in this case
Thanks, I will actually do that. (Forgot about Invoking methods) But, I still would like to find out why this wouldn't work.
What happens when you change this line inventory.GetComponent ().StartCoroutine(coroutine); to StartCoroutine(coroutine); ? A future problem i see is that the code in On$$anonymous$$ouseOver will call the routine probably unintended multible times.
I dont know what dev tools you use but you maybe should debug this proper by setting a breakpoint and step-in with Visual Studio for example, and the breakpoint set in the On$$anonymous$$ouseOver() in line 2 would make sense
Answer by LilGames · Apr 22, 2018 at 08:44 PM
What do you mean when you say "not working" ? It would help if you identified what problem you're having.
Is there a reason you are using WaitForSecondsRealtime and not WaitForSeconds(5) ??
$$anonymous$$y problem is simply that WaitForSeconds is not working. I tried both RealTime and the normal one, but it just doesn’t wait.
Answer by Instant_Dave · Apr 23, 2018 at 03:50 PM
I'm new but I'd like to help if I can.
public class Whatever : MonoBehaviour { public float delay = 1.5;
void Start()
{
OnMouseOver(); // I use Button also StartCoroutine (LoadStuffAfterDelay (delay)); } IEnumerator LoadStuffAfterDelay(float delay) { yield return new WaitForSeconds (delay); // //Do this next directions here
} }
Well that didn't post right lol.
public float delay = 75;
void Start()
{
StartCoroutine (LoadAfterDelay (delay));
}
IEnumerator LoadAfterDelay(float delay)
{
yield return new WaitForSeconds (delay);
// Do next directions here
}
}
Add to On $$anonymous$$ouse Over and modify scripts a little. 2nd script pointed to from first script after coroutine delay something like
public Class CheckIfStillHover() {
if still hover = true
print ("Fine. I admit it. I cut in line. Happy?");
}