Problem with vector 3 transform.

I’m trying to move my player gameobject to a new location and cannot work out what is going wrong.

This code places it exactly where i want it to be:

void Start()
    {
        
        gameObject.transform.position = new Vector3(0, 15, -330);

    }

However, what i need is for the location to be stored in a variable so i can change it later on.

When i inset the same vector 3 into the variable like this; my gameobject is moved to a totally different location even though it is using the same vector 3 and I cannot work out why or what I need to do to fix it.

public class PlayerController : MonoBehaviour
{
 
  public Vector3 startLocation = new Vector3(0, 15, -330);

void Start()
    {
       
   
        gameObject.transform.position = startLocation;

    }

keep in mind that public variables in MonoBehaviour are automaticly serialized and connected to the unity inspector window

as in, when you click the object in the scene, you can see the startLocation variable and its value in the inspector
changes there are saved and will overwrite the initial value in the variable declaration in your script

so make sure that when you click the object in the scene and look at the startLocation variable, it actually has the (0, 15, -330) as its value, and not something else you had previously and forgot about

3 Likes

This depends on what you used to have as startLocation’s value.

When you have a public variable, that variable is serialized. I’m guessing that when you first wrote that part of the script, you had either this:

public Vector3 startLocation;

or some different value:

public Vector3 startLocation = new Vector3(1,2,3);

The first time the script is compiled with that line of code, Unity notices that you have a field called startLocation, finds every PlayerController script attached to an object in your scene, and adds the startLocation field with its default value - in the above two examples, that would be (0,0,0) and (1,2,3) respectively. That initial setting is the only time when the value after the = sign matters, because once startLocation’s value is saved in the scene, it’s the scene who has control over that field’s value. So if you started with (1,2,3), and then changed those numbers to (0,15,-330) in the script, the object that already existed in your scene already has its value, so it doesn’t bother to check the script for the default value.

You can either:
a) find the object and change the Vector3 in the inspector
b) change this field to private instead of public. Private fields aren’t serialized (unless you explicitly make them so), so now there will be no startLocation value saved in the scene, and it’ll just use the script’s value.
c) find the object, right-click on the top bar of the copmonent, and click “Reset” (which will reset ALL fields to their default value)

…all that said, it’s usually inconvenient to store Vector3 positions as numbers that you type in the inspector. Most likely, you’ll be better off if you use a reference to some Transform (which might be just an empty object), which you can move around in the Scene View:

public Transform startPositionTransform;

void Start() {
transform.position = startPositionTransform.position;
}

In the case of starting position, I’m guessing that the reason you’re doing this is because the player is going to get reset at some point, and you want to move back to the start position? If so, there’s an even better way:

private Vector3 startPosition;

void Start() {
startPosition = transform.position;
}

Now you can reset the position with transform.position = startPosition; and it will automatically put the object back to where you placed it when you saved the scene - no need to type in those numbers at all.

2 Likes

thanks for the response, looks like this was the problem I was having, still getting to grips with the basics :slight_smile:

1 Like

thanks for the detailed response, it helped me understand what’s going on :slight_smile:

I’m wanting to implement a checkpoint system in my game, where if the player passes a certain checkpoint the respawn position changes so they don’t have to start over from the beginning.
I’m still in the process of researching how best to accomplish this, but for the method i was going to attempt, i was going to pass my player script a new spawnlocation upon reloading the scene.

For a checkpoint system if you’re just looking for something relatively simple OnTriggerEnter would probably work really well.

I would then maybe use a boolean or an integer to indicate whether the player has entered if you need to access information based on that afterwards.