WaitForSeconds don't work with variables from another scripts
Hi, I'm having some problems with "WaitForSeconds", this is my code:
IEnumerator SpawnMuro()
{
while (Weird == true)
{
Instantiate(Muro, new Vector2(transform.position.x, transform.position.y+1.3f), Quaternion.identity);
yield return new WaitForSeconds(1.53f / Objetos.Controle);
}
yield return null;
}
When the code hits the WaitForSeconds, stay in it forever, but:
//Objetos.Controle value is 0.1f and is a public static float in another script.
Debug.log(1.53f / Objetos.Controle); //returns 15.3 as expected
yield return new WaitForSeconds(Random.Range(2, 5)); //No Problems here
yield return new WaitForSeconds(15.3f); //No Problems here
yield return new WaitForSeconds(1.53f / 0.1f); //No Problems here
yield return new WaitForSeconds(1.53f / Objetos.Controle); //probably resulting infinity.
yield return new WaitForSeconds(1.53f / (float)Objetos.Controle); //Not working
ComplexMath = 1.53f / Objetos.Controle //still returning 15.3 as expected
yield return new WaitForSeconds(ComplexMath); //Not working
What I'm doing wrong?
Answer by K-Skanz · Oct 21, 2017 at 05:12 PM
My solution for this:
void Start () {
StartCoroutine(SpawnMuro(Objetos.Controle));// My solution, send the variable like this;
}
IEnumerator SpawnMuro(float var)//and catch it here;
{
while (Weird == true)
{
Instantiate(Muro, new Vector2(transform.position.x, transform.position.y+1.3f), Quaternion.identity);
yield return new WaitForSeconds(1.53f/var); //and use here;
}
yield return null;
}
(But I still don't understand why in my first code the WaitForSeconds don't work. I printed a Debug.Log(Objetos.Controle) right before the WaitForSeconds and returns 0.1f as expected, put it into WaitForSeconds and doesn't work anymore.)
Answer by MaxGuernseyIII · Oct 21, 2017 at 12:39 AM
WaitForSeconds has no way of knowing how you calculated the float you passed in so it must be something wrong with the calculation, itself. Try using Debug.Log on both the numerator and the denominator and you will at least see what the offending values that are breaking the calculation are.
As far as why they are wrong, there are too many possibilities to hazard a meaningful guess. A lot of the time people are having a problem like this, though, it's because the calculation is occurring before one of the values is initialized. So a Debug.Log statement that tells you when the broken input gets broken and/or fixed will probably help a lot, too.
Well, after a lot of time I have solved the problem with doing this:
void Start () {
StartCoroutine(Spawn$$anonymous$$uro(Objetos.Controle));
}
IEnumerator Spawn$$anonymous$$uro(float var)
{
while (Weird == true)
{
Instantiate($$anonymous$$uro, new Vector2(transform.position.x, transform.position.y+1.3f), Quaternion.identity);
yield return new WaitForSeconds(1.53f/var);
}
yield return null;
}
But, now I have the same problem, but in another way, I need to stop the coroutine at some point, and to do this I tried with StartCoroutine ("Spawn$$anonymous$$uro") and StopCoroutine("Spawn$$anonymous$$uro"), but I can't send the "Objetos. Controle" with this, and then I tried what is in the Docs of Unity C#:
private IEnumerator Co$$anonymous$$uro;
void Start () {
Co$$anonymous$$uro = Spawn$$anonymous$$uro(Objetos.Controle);
StartCoroutine(Co$$anonymous$$uro);
//Codes.......
StopCoroutine(Co$$anonymous$$uro);
}
But it doesn't recognize Objects.control as anything, this think I'm sending nothing with this, someone knows how making this work?
What do you mean by "it doesn't recognize Objects.control as anything"? Do you mean you get a null reference exception? Do you mean that the variable is zero when you think it should be another value?
You really aren't giving us very much information with which to help you. You might want to start a forum thread so people can have a back-and-forth conversation. This venue is for non-open-ended questions.
If you do start a forum thread, do yourself a favor and post all the relevant code.
Sorry, english is not my primary language.
But this time the problem is in my code, I Started the coroutine before set the Objetos.Controle as 0.1f
Thank you for your help, it's finally working.
(But I still don't understand why in my first code the WaitForSeconds don't work. I printed a Debug.Log(Objetos.Controle) right before the WaitForSeconds and returns 0.1f as expected, put it into WaitForSeconds and doesn't work anymore.)
Your answer
Follow this Question
Related Questions
How to stop a big float value from changing to weird equations 0 Answers
How can I use a float Variabel in an other Sript? 1 Answer
Waiting for fading to end before switching scenes 0 Answers
Unity WaitForSeconds Not Working Inside Update or a Loop 2 Answers
How can I change this script so that each instantiated prefab spawns a set amount faster 0 Answers