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);
}
}