So I’m following along with a course on Udemy. I have 2 scenes. A Test and Test2 scene. I can go back and forth between scenes but my player appears on the new scene based on where he was on the screen. I made an empty object called Area Entrance for the Test 2 Scene and attached the following script to it…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AreaEntrance : MonoBehaviour {
public string transitionName;
// Use this for initialization
void Start () {
if(transitionName == PlayerController.instance.areaTransitionName)
{
PlayerController.instance.transform.position = transform.position;
}
}
// Update is called once per frame
void Update () {
}
}
So basically I should be able to move the Area Entrance object anywhere on the Test 2 scene and whenever I enter that scene my player should be where ever the Area Entrance object is. But my player appears in the same spot where he was in the previous scene.
I have 2 other scripts associated with this script/object in my PlayerController script and my AreaExit script. I’m assuming I’ve made a spelling issue or something but I’m not getting any errors anywhere so I’m not sure. Here’s my two other scripts…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public Rigidbody2D theRB;
public float moveSpeed;
public Animator myAnim;
public static PlayerController instance;
public string areaTransitionName;
// Use this for initialization
void Start () {
if(instance == null)
{
instance = this;
} else
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
}
// Moving the Player
void Update () {
theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * moveSpeed;
myAnim.SetFloat("moveX", theRB.velocity.x);
myAnim.SetFloat("moveY", theRB.velocity.y);
if(Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1 || Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
{
myAnim.SetFloat("lastMoveX", Input.GetAxisRaw("Horizontal"));
myAnim.SetFloat("lastMoveY", Input.GetAxisRaw("Vertical"));
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class AreaExit : MonoBehaviour {
public string areaToLoad;
public string areaTransitionName;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Player")
{
SceneManager.LoadScene(areaToLoad);
PlayerController.instance.areaTransitionName = areaTransitionName;
}
}
}
Also I’m using Unity 2018.2.11f1 because that’s the same version the course is in. Any help would be appreciated, I’ve been trying to figure this out for about 12 hours now