- Home /
refer to current coroutine
So I have this RTS style game with ore mining, for which I'm a little confused on how to setup the system, so that the ming unit have to stay at the current task for a while until the ore is mined.
.
The outline of how it should work is :
.
A coroutine is started at the resource object (Ore pile). this routine will be idle for 5 sec, but does check every 0.1 sec if the mine is depleted, and if the unit is still mining it. when the time has passed, an ore will be added to the units inventory.
.
What I can't figure out is how to stop the coroutine if the unit is doing something else, and how to avoid the same unit starting multiple simultaneous routines.
.
So far I have made a Coroutine currentJob
-variable in the UnitController
-script, where the idea was to compare the current coroutine to the stored one, to see if they are the same, but i don't know if there is a smarter method?
.
Any suggestions is appreciated?
-Mikael
Since you have a reference to the running coroutine in the currentJob
variable, it's easy. When you start a new coroutine, you can check currentJob
. If it isn't null, you can make StopCoroutine(currentjob);
and then set current job to null. After this you can start a new coroutine.
Answer by mikawendt · Oct 02, 2019 at 04:20 PM
@GubaLord 's solution is probably to prefer, however, I had such problems trying to store a coroutine i an variable that I took a different approach, in the beginning of a coroutine I generated a large random number, and stored it on the mining unit, then I checked at the end of the coroutine if the generated and store numbers were the same eg. if another task have been performed meanwhile. This does the job
Your answer
Follow this Question
Related Questions
constructing buildings rts style. 1 Answer
How to find a font via script? 2 Answers
Is there a way to measure the pixel with/height of a string with a given font? 2 Answers
Removing the last element in an ArrayList; C# 1 Answer
How can you use a Coroutine and WaitForSeconds to repeat actions? 1 Answer