You could do something like this:
void Start()
{
print( "Starting " + Time.time );
StartCoroutine( WaitPrintAndSetValue( 2.0F, theNewValue => example.Variable = theNewValue ) );
print( "Before WaitAndPrint Finishes " + Time.time );
}
/// <summary>Wait for the specified delay, then set some integer value to 42</summary>
IEnumerator WaitPrintAndSetValue( float waitTime, Action<int> setTheNewValue )
{
yield return new WaitForSeconds( waitTime );
print( "WaitAndPrint " + Time.time );
int newValueToSet = 42;
setTheNewValue( newValueToSet );
}
If after your delay you need to both read and update a value, you could e.g. pass Func<int> readTheOldValue, Action<int> setTheNewValue
and invoke with the following lambdas () => example.Variable, theNewValue => example.Variable = theNewValue
Here's more generic example:
void Delay( float waitTime, Action act )
{
StartCoroutine( DelayImpl( waitTime, act ) );
}
IEnumerator DelayImpl( float waitTime, Action act )
{
yield return new WaitForSeconds( waitTime );
act();
}
void Example()
{
Delay( 2, () => {
print( "After the 2 seconds delay" );
// Update or get anything here, however you want.
} );
}