C# : Pass a parameter to a method by reference?

How can I do something similar to the following.

I have a Function that modifys an int. For simplicity sake, lets say it generates a random number.

    static int GenRand()
        Random random = new Random();
        return( random.Next(0, 100));

Now instead of returning a random number I want to pass a property of an object. For example Go.transform.position.x or Go.light.intensity.

(I’m aware the following does not work)

        static void GenRand(ref int PropertyToChange)
            Random random = new Random();
            PropertyToChange = random.Next(0, 100));

GenRand(ref Go.transform.position.x);

My actual need is far more complex, but I’m looking for a general way to pass a arbitrary property to an function and have it modified by the result.

In other languages I would use a pointer(can’t to my knowledge in Unity), or by reference.

Any thoughts?

I see two problems with the bottom

  1. passing a float where an int is expected

  2. transform.position is an accessor, not a variable. That means transform.position.x cannot be directly modified and attempting to pass it as a ref will be (correctly) detected as an error.

However ref works exactly how you showed in GenRand. For instance the following will randomize the position of whatever gameobject it is attached to

using UnityEngine;

public class ExampleClass : MonoBehaviour {
    void Start() {
        Vector3 position=transform.position;
        GenRand(ref position.x);

    static void GenRand(ref float PropertyToChange) {
        Random random = new Random();
        PropertyToChange = Random.Range(0, 100);

" Go.transform.position.x " you can’t directly change just the X value you have to set the whole position Vector3 value

Otherwise using ref should work for you