Hi at the start of my game a script runs that looks for 3 tanks in the start function but as my level is generated after the start of the script it cant find them i cant use update as when the tanks die i use the and it cant find them as they are gone
is their away to find the tanks later? but still only find them once
You could also just use start as an IEnumerator
IEnumerator Start()
{
yield return new WaitForSeconds(2f);
//functionality here
}
Use MonoBehaviour.Invoke.
You can invoke a function from your Start() function that will run after a specific time delay.
Something like:
void Start()
{
Invoke("CountTanks", 2);
}
void CountTanks()
{
// Count your tanks here
}
Here the CountTanks function will execute after 2 seconds.
Ive seen people use delay, but what about having a boolean in some script that u can call?
using an ienumerator called from start that checks a boolean every frame/second and continues when its changed (meaning the level is loaded).
meanwhile you have all level loading scripts change booleans when they are done in some manager script and when all are done the manager will return ‘done’.
let me know if u need example code.
I got around this by creating public heigh/width floats, then assiging them in start, then using those variables. This only worked for my on 1/2 of my scripts so IDK. Good luck
Throwing the code in a LateStart function then invoking that a second later fixes the fix. but thats problematic. (also i see 2014 post date this is for future readers)
There will be instances where you can’t make start an IEnumerator. For example, let say you are inheriting the Unity’s Button class. You need to override the void Start() from the base Button class and cannot make it an IEnumerator. If you still want to wait for something to happen you can accomplish this using a two coroutines.
For example lets say you don’t want Start on one script to run until Start on another script has run. Basically an exucution order. You can do something like this basically delaying the start function.
public class ButtonClass : Button
{
private bool canContinue = false;
protected override void Start()
{
base.Start();
StartCoroutine(WaitToExecute());
}
private IEnumerator WaitToExecute()
{
SomeOtherClass soc = FindObjectOfType<SomeOtherClass>(); //Probably shouldn't get the reference to the other class like this.
yield return StartCoroutine(CheckCanContinue(soc)); //Wait for CheckCanContinue to finish running.
//At this point we know that Start() on SomeOtherClass has finished running and doing stuff.
//We can now do stuff.
}
private IEnumerator CheckCanContinue(SomeOtherClass otherClass)
{
while(!otherClass.isStartFinished)
{
if(canContinue)canContinue = !canContinue; //If can continue is true set it to false.
yield return WaitForEndOfFrame();
}
canContinue = true; //The Start() of otherClass is finished running so set canContinue to true.
}
}
public class SomeOtherClass : MonoBehavior
{
private bool isStartFinished = false;
public bool isStartFinished{ get => isStartFinished;}
void Start()
{
//Do stuff here.
isStartFinished = true; //This goes at the end of start.
}
}