- Home /
My code is getting stuck in Waitforseconds even though am not destorying the object or setting it inactive?,Am Stuck in WaitforSeconds
My Goal: is to swap the highlghtedcolor with the selectedTargethighlight and then wait for 1 second before the selected target loses 1 HP and pass the turn over to the player.
My problem is that when the code is running and i meet the condition for the coroutine to start, it doesnt get pass the waitforseconds.
The object is not been destory or been set inactive in any way shape or form.
This is my code:
// Update is called once per frame
void Update()
{
if (GetComponent<BattleSystem>().Playerturn == false)
{
/*print("Enemy turn");
GetComponent<BattleSystem>().highLightedColor.transform.position = GetComponent<BattleSystem>().BattleObjects[GetComponent<BattleSystem>().playercheck].transform.position;
GetComponent<TargetSelection>().selectedTargetHighlight.transform.position = GetComponent<TargetSelection>().heros[num1].transform.position;*/
StartCoroutine(holdforhighlightswap());
/*GetComponent<TargetSelection>().heros[num1].GetComponent<PlayerStates>().hp -= 1;
GetComponent<BattleSystem>().Playerturn = true;
GetComponent<TargetSelection>().enabled = true;*/
}
}
IEnumerator holdforhighlightswap()
{
print("Enemy turn");
GetComponent<BattleSystem>().highLightedColor.transform.position = GetComponent<BattleSystem>().BattleObjects[GetComponent<BattleSystem>().playercheck].transform.position;
GetComponent<TargetSelection>().selectedTargetHighlight.transform.position = GetComponent<TargetSelection>().heros[num1].transform.position;
yield return new WaitForSeconds(1.0f);
print("I waited 1 second");
GetComponent<TargetSelection>().heros[num1].GetComponent<PlayerStates>().hp -= 1;
GetComponent<BattleSystem>().Playerturn = true;
GetComponent<TargetSelection>().enabled = true;
}
your coroutine is getting called multiple times in the Update function as Playerturn remains false till 1 second according to your code.
Answer by Nevermiss · Jun 16, 2020 at 07:38 PM
Hi, the coroutine seems just fine.
Does the "I waited 1 second" get printed?
Add another print just before 'yield return new ..' and make sure everything works so far.
Check whether the coroutine gets called everyframe or just once (it's in update thus it will start a new coroutine each frame while GetComponent().Playerturn == false).
My guess is that some of the GetComponent's are not working, thus the coroutine encounters an error there and stops.
Another thing you must start using is caching, so the game is much more performant:
private BattleSystem bs;
private TargetSelection ts;
void Start()
{
bs = GetComponent<BattleSystem>();
ts = GetComponent<TargetSelection>();
}
void Update()
{
if (bs.Playerturn == false)
{
StartCoroutine(holdforhighlightswap());
}
}
IEnumerator holdforhighlightswap()
{
print("Enemy turn");
bs.highLightedColor.transform.position = bs.BattleObjects[bs.playercheck].transform.position;
ts.selectedTargetHighlight.transform.position = ts.heros[num1].transform.position;
yield return new WaitForSeconds(1.0f);
print("I waited 1 second");
ts.heros[num1].GetComponent<PlayerStates>().hp -= 1;
bs.Playerturn = true;
ts.enabled = true;
}
Not only the code looks cleaner, it also runs 5x faster each time you start the coroutine.
Answer by RodrigoAbreu · Jun 28, 2020 at 11:29 AM
Hey @ethensanchez7312,
Make sure to retrieve all of your needed components in the Start instead of using GetComponent each frame and also inside your Coroutine, as @Nevermiss has described.