reference to Vector3 from another class gives an empty Vector3.zero,referenced Vector3 returns 0 when used in another class

I’m programming “ports” for my ship to sit when a level is completed. I’m trying to stay organized by putting the Vector3 in another class and referencing it in a different class that will loop through the completed levels to determine which Vector3 to use. All the Vector3 coordinates are defined in void Awake() and are called to move the position of the ship in the other class in void Start(). But when I assign the Vector3 to the ships position it puts it at 0.0, 0.0, 0.0 which is far from where I want it. What is going on?


defineCoords.cs:
public class defineCoords : MonoBehavior {
    public static Vector3 level1Port;

    void Awake(){
        level1Port = new Vector3(-10.0f, 10.0f, 10.0f);
        print(level1Port);
        //prints {-10.0, 10.0, 10.0 }
    }
}

positionShip.cs

public class positionShip : MonoBehavior{

    private int currentLevel;
    private defineCoords coords;

    void Start(){
        currentLevel = 1;
        coords = new defineCoords();
        switch(currentLevel){
        case 1:
            print(coords.level1Port);
            //prints 0.0, 0.0, 0.0
            GameObject.Find("explorerShip").transform.position = coords.level1Port;
            break;
        }
    }
}

You (edit) shouldn’t create a descendant of MonoBehaviour like that. MonoBehaviours are intended to be used as components on GameObjects, which have their functionality (in this case Awake()) dependent on the object they’re attached to. If you just want a class to store your Vector3, you can just use a regular class.


public class defineCoords {
     public Vector3 level1Port;
 
     public defineCoords (){
         level1Port = new Vector3(-10.0f, 10.0f, 10.0f);
         print(level1Port);
         //prints {-10.0, 10.0, 10.0 }
     }
 }

In which case, your code should work fine.

You (edit) shouldn’t create a descendant of MonoBehaviour like that. MonoBehaviours are intended to be used as components on GameObjects, which have their functionality (in this case Awake()) dependent on the object they’re attached to. If you just want a class to store your Vector3, you can just use a regular class.


public class defineCoords {
     public Vector3 level1Port;
 
     public defineCoords (){
         level1Port = new Vector3(-10.0f, 10.0f, 10.0f);
         print(level1Port);
         //prints {-10.0, 10.0, 10.0 }
     }
 }

In which case, your code should work fine.

Switch to this:

 public class defineCoords : MonoBehavior {
     public static Vector3 level1Port;
 
     void Awake(){
         level1Port = new Vector3(-10.0f, 10.0f, 10.0f);
         print(level1Port);
         //prints {-10.0, 10.0, 10.0 }
     }
 }

you need a level1port as static var.

and this:
public class positionShip : MonoBehavior{

     private int currentLevel;
     //private defineCoords coords;
 
     void Start(){
         currentLevel = 1;
         //coords = new defineCoords();// cannot define a object, all variables start in new memory adress in object
         switch(currentLevel){
         case 1:
             print(defineCoords.level1Port);
             //prints 0.0, 0.0, 0.0
             GameObject.Find("explorerShip").transform.position = defineCoords.level1Port;
             break;
         }
     }
 }