Cannot modify the return value of Transform.position

Pretty much this is the code for playing a short animation and switching scenes when interacting with a collider at the side of a scene. The switching between scenes is fine but I recently added this

scenes = new float[1, 2];
        scenes[currentScene, 0] = thePlayer.transform.position.x - 1;
        scenes[currentScene, 1] = thePlayer.transform.position.y;

And this

thePlayer.transform.position.x = scenes[currentScene, 0];
        thePlayer.transform.position.y = scenes[currentScene, 1];

And Unity is saying this: Cannot modify the return value of ‘Transform.position’ because it is not a variable

I’m unsure why it isn’t working as I am able to change the player’s position if I use a specific objects position but not the variable. The full code is below. What am I doing wrong?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class sceneChanging : MonoBehaviour
{
    public int sceneToLoad;
    public Animator aSceneTransition; //Defines the variable that will hold the reference for the animation used
    public float transitionTime = 3f; //Defines the time taken to complete the animation as 5 seconds
    public float[,] scenes; // array as I cannot serialize Unity variables such as Vector2
    public int currentScene;
    public GameObject thePlayer;
      


    private void OnTriggerEnter2D(Collider2D collision) // On entering the collider
    {
        scenes = new float[1, 2];
        scenes[currentScene, 0] = thePlayer.transform.position.x - 1;
        scenes[currentScene, 1] = thePlayer.transform.position.y;
        LoadNextScene();
    }

    public void LoadNextScene()
    {
        //Executes a coroutine that calls the function LoadScene() using the the index of the current Unity scene + 1
        StartCoroutine(sceneTransition(sceneToLoad));
        thePlayer.transform.position.x = scenes[currentScene, 0];
        thePlayer.transform.position.y = scenes[currentScene, 1];

    }

    IEnumerator sceneTransition(int scene)
    {
        //Plays the animation when the 'Start' condition is triggered
        aSceneTransition.SetTrigger("Start");

        //Doesn't begin the new animation until the transition time defined is over
        yield return new WaitForSeconds(transitionTime);

        SceneManager.LoadScene(scene);
    }
}

In LoadNextScene you have this:

thePlayer.transform.position.x = scenes[currentScene, 0];
thePlayer.transform.position.y = scenes[currentScene, 1];

Try changing to:

thePlayer.transform.position = new Vector3(scenes[currentScene, 0], scenes[currentScene, 1], thePlayer.transform.position.z);
2 Likes

learn more about value types in C#, as you cannot change their fields directly in such scenarios.
value types are specified with the keyword struct instead of class in design-time, and behave pretty much like primitive values do, they are copied around instead of being passed by reference.

value types are extremely lightweight and allocated differently, so you shouldn’t worry about that new clause consuming memory or making anything slow (like it would with normal objects), but you have to supply the object in its entirety to reflect the change.

Unity uses them for simple data constructs such as Vector2, Vector3, Plane, Ray, RaycastHit, Bounds and so on.