I am boxing an integer as an object (System.Object). Then if I assign a new value to the object within the same scope, the value of the object is getting updated. But, if I try to pass the object as an argument in another function and try to assign a new value to it, the value is not getting updated. I was wondering if the object is an instance of the System.Object class and it's a reference type, then why the value of the object is not getting updated when I pass it as an argument in another function?
using UnityEngine;
public class BoxTest : MonoBehaviour
{
void Start()
{
int i = 10;
object obj = i;
// Output : 10
Debug.Log(obj);
obj = 20;
// Output : 20
Debug.Log(obj);
changeValueInBoxedObject(obj);
// Output : 20, expecting 30
Debug.Log(obj);
}
void changeValueInBoxedObject(object obj)
{
obj = 30;
}
}