Unity 3D C# - Code Timer

I was wondering if it is possible for a code in

void Start(){
	weaponScript = GetComponent<Weapons>();




To have like a coded timer in the code that will set off the debugs in like 2 seconds?
I know there is something similar to this in MS visual C# I think its called threading.

The reason I need this is because I got another code in another script I got a array that sets it’s self in the void start and this is the other script trying read that array on start also, so i think it needs to wait like 2 secs or something.

Can I please have some help?

private void Start()

private IEnumerator DebugLog(float waitingTime)
        //your debug code
        yield return new WaitForSeconds(waitingTime);


As always, there are several ways to make this.

First, you can make array’s initialization in another script in Awake() method instead of Start(). Awake is pretty much the same, but it happens before Starts. Here is MonoBehaviour.Awake() description.

Also you can manually adjust the order of execution of scripts, but I never did it and do not recommend it in your case because it can break again. And it’s not funny at all. :slight_smile:

The second way is to make a script with an array have a property, called something like ‘isArrayInitialized’, then make your script run a Coroutine, which will check if the array is initialized and use it and stop itself once it is done.

And the third and most versatile and robust way, I think, is to make that script with an array have a property, again called ‘isArrayInitialized’, but instead of running a coroutine you will check if an array is initialized, and if it’s not, you will do the trick with events. Let’s say you add to a script with array a Delegate, which will be called by that script with array right after array’s initialization. So when you check if the array is initialized and it appears to be not done yet, you just subscribe to the event, which will pass the array once the array is ready. That is fun! :slight_smile: