Swapping values randomly every 2 seconds.
Hello!
I'm trying to create a script that swap a value every two seconds, and it randomly chooses the values between 1 and 0;
My issue is that it only swaps it once. and not continuously (and not even randomly).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class talknWalkSwap : MonoBehaviour
{
int swap;
// Start is called before the first frame update
void Start()
{
StartCoroutine(ChangeTag());
Debug.Log(swap);
}
// Update is called once per frame
void Update()
{
}
IEnumerator ChangeTag()
{
var wait = new WaitForSeconds(3);
while (true)
{
yield return wait;
swap = 1;
yield return wait;
swap = 0;
}
}
}
Comment
I believe it does, you just print the value of swap
once. Put the Debug.Log
in the coroutine ins$$anonymous$$d.
I put it in the coroutine, it doesn't swap the values.
Then, why does it swap when I try in my project?
while (true)
{
yield return wait;
swap = 1;
Debug.Log(swap);
yield return wait;
swap = 0;
Debug.Log(swap);
}
($$anonymous$$ake sure Collapse
is not checked in the console tab)
Your answer
Follow this Question
Related Questions
How to swap any two gameobjects in a field area? 0 Answers
Character swap breaks movement. 1 Answer
C# Adding random values to array 1 Answer
how to change the animation lengh by script? 0 Answers
Script Spawning Double? 1 Answer