Passing a pointer to a function in C#?

So, I’ve got what I thought would be a simple problem. I have a function that takes a variable, a value, and a delay, and adds the value to the variable, after delay seconds. It has to be an IEnumerator to I can have the delay.

But how do I pass the variable to the function?

	void Start ()
	{
		delayedStatisticUpdate (health, 5, -1);
		delayedStatisticUpdate (hunger, 50, 10);
		delayedStatisticUpdate (energy, 1, -10);
		delayedStatisticUpdate (level, 10, 1);
	}

	public static IEnumerator delayedStatisticUpdate (int *statistic, int delay, int change)
	{
		yield return new WaitForSeconds(delay);
		*statistic += change;
		Debug.Log("delayedUpdate happened");
	}

Obviously my start and update functions are far more complex, and called delayedStatistcUpdate is being called from multiple scripts, but thats not the issue here.

I’m getting 2 errors:
error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
and
error CS1637: Iterators cannot have unsafe parameters or yield types

Could anyone tell me how I SHOULD be doing this?

What you are looking for are delegates in C#

And get rid of that int *blabla thats a pointer thats not allowed at all.
if you want to change the value, the correct thing would be ref int statistic (look into the ref and out keywords)

Okey dokey, I’ll look into that, thanks Dreamora.

My first language was C, and I’m realtively new to C#, so it was kind of my instinct. Thanks for pointing me (pun definitely intended) in the right direction

Yes in your case “delayedStatisticUpdate (ref int statistic, …” is going to do it…

And you could use pointers in some hacky way by doing “public static unsafe IEnumerator delayedStatisticUpdate (int *statistic,…” and renaming “mcs.exe” to something else, replacing it by a dummy process which adds the “unsafe” compilation flag to Unity compilations, which in turn would exclude you from webplayer and mobile (I guess)…

I don’t think it’s possible to use unsafe mode in Unity at all. In any case you don’t need pointers, so it’s basically irrelevant.

–Eric