A question about direct references...

Lets say i have a class A which is instantiated as an object in a GameObject’s monobehaviour. The class A is supposed to have a direct control over the position of the GameObject (the monobehaviour’s gameObject.transform.position member).
I tried to do something like this in the monobehaviour.

    using UnityEngine;
    using System.Collections;
    using Mynamespace; //contains the class A
    
    public class MyMonobehaviour : MonoBehaviour {
    
        public Vector3 myReference;
        public A sampleObject;

        void Start () {
            myReference = gameObject.transform.position;
            sampleObject = new A(ref myReference);
         }

     }

the class A is something like this:

public class A {

       public Vector3 directReference;

        public A (ref Vector3 temp) {              
            directReference = temp;
        }

        public void Access() {
              Debug.Log("Current X position:  " + directReference .x);
        }
    }

the question is, why it doesnt provide direct access to the GameObject’s position? if i try something like sampleObject.Access(); it wont display the current position of the gameobject, only the position it had at the moment of the A object instantiation.

Sorry for the long question but all of this driving me crazy, im a beginner, thanks.

I am not an expert in this field, but I think the problem comes from the fact Vector3 is a struct (value type) and not a class (reference type). Thus, when you assign a Vector3 to another, you create a copy, you don’t get the reference to the vector.

To fix your problem, I suggest to use directly the Transform component, and call transform.position.