- Home /
Question by
DJGhostViper · Nov 23, 2016 at 03:26 AM ·
coroutinewaitforsecondsrtsresourcecollection
How can you use a Coroutine and WaitForSeconds to repeat actions?
Im making an RTS game where there are resource collectors that take resources from other gameobjects... I want there to be a pause when they are taking the resources so they don't get filled up so fast. I need help with making the unit collect and wait a couple seconds then collect again then repeat until it is filled up.
public IEnumerator Harvest()
if (PlasticCarryAmount < 30) {
this.gameObject.GetComponent<ResourceCollection> ().PlasticCarryAmount += 10; //Add this to carry amount
yield return new WaitForSeconds (3); //Wait a couple seconds then repeat until PlasticCarryAmount is not less than 30
} else
{
Harvesting = false;
}
}
Comment
Best Answer
Answer by aditya007 · Nov 23, 2016 at 04:47 AM
Just put a while condition
while (true) {
if (PlasticCarryAmount < 30) {
this.gameObject.GetComponent<ResourceCollection> ().PlasticCarryAmount += 10; //Add this to carry amount
yield return new WaitForSeconds (3); //Wait a couple seconds then repeat until PlasticCarryAmount is not less than 30
} else
{
Harvesting = false;
break;
}
}
}
This will repeat till your if condition stays true.
Your answer
Follow this Question
Related Questions
How can you make an AI perform specific movements? 2 Answers
WaitForSeconds problem with Unity Pro 3 Answers
yield WaitForSeconds compatibility? 0 Answers
WaitForSeconds does not work 1 Answer