I come from a C++ background and without pointers I’m slightly lost on how to solve this problem. I need objectA in Unity to hold a reference to another objectB (This is better for memory as multiple objectAs will need the exact same objectB). How would I accomplish this?
Yes, however only to certain extent. Pointers in C# can only hold values to arrays or value types.
To declare: type *nameOf and then with & you get the memory address.
int largeNumber = 100;
int *ptr = &largeNumber;
then (int)ptr displays the memory address and *ptr displays the value at that memory address
In C#, you need to forget about pointers, they should only be used in really specific situation like hardware and driver access.
In Unity, you are likely to use them in few situations. When dealing with native code communication, IntPtr is more likely used.
If you need an object to hold another object address, then think of C++ references without the &
MyType objA = new MyType(); MyType objRef1; objRef1 = objA;
Now your objRef1 points to the same address as objA.
objRef1.item = newValue; print(objA.item); // This prints the newValue content
You can also box/unbox value type-
int myInt = 10; object obj = (object)myInt; print((int)obj); // prints 10
The problem with boxing is that you need to know the type you are dealing with.