Search Unity

Resolved Changing scenes, player not starting where they're supposed to.

Discussion in '2D' started by UncleLeo, Oct 27, 2020.

  1. UncleLeo

    UncleLeo

    Joined:
    Mar 23, 2020
    Posts:
    7
    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...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AreaEntrance : MonoBehaviour {
    6.  
    7.     public string transitionName;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         if(transitionName == PlayerController.instance.areaTransitionName)
    12.         {
    13.             PlayerController.instance.transform.position = transform.position;
    14.         }
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.        
    20.     }
    21. }
    22.  
    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....

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public Rigidbody2D theRB;
    8.     public float moveSpeed;
    9.  
    10.     public Animator myAnim;
    11.  
    12.     public static PlayerController instance;
    13.  
    14.     public string areaTransitionName;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         if(instance == null)
    19.         {
    20.             instance = this;
    21.         } else
    22.         {
    23.             Destroy(gameObject);
    24.         }
    25.  
    26.         DontDestroyOnLoad(gameObject);
    27.     }
    28.    
    29.     // Moving the Player
    30.     void Update () {
    31.         theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * moveSpeed;
    32.  
    33.         myAnim.SetFloat("moveX", theRB.velocity.x);
    34.         myAnim.SetFloat("moveY", theRB.velocity.y);
    35.  
    36.         if(Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1 || Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
    37.         {
    38.             myAnim.SetFloat("lastMoveX", Input.GetAxisRaw("Horizontal"));
    39.             myAnim.SetFloat("lastMoveY", Input.GetAxisRaw("Vertical"));
    40.            
    41.         }
    42.     }
    43. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class AreaExit : MonoBehaviour {
    7.  
    8.     public string areaToLoad;
    9.  
    10.     public string areaTransitionName;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.        
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.        
    20.     }
    21.  
    22.     private void OnTriggerEnter2D(Collider2D other)
    23.     {
    24.         if(other.tag == "Player")
    25.         {
    26.             SceneManager.LoadScene(areaToLoad);
    27.  
    28.             PlayerController.instance.areaTransitionName = areaTransitionName;
    29.         }  
    30.     }
    31. }

    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 :(
     
  2. UncleLeo

    UncleLeo

    Joined:
    Mar 23, 2020
    Posts:
    7
    Update: It was a spelling issue in the inspector. My Area Transition Name in Area Exit didn't match the Transition Name in Area Entrance. Though I didn't see the difference, I copy/pasted from the first and when it prompted me to save my changes I figured that was it. It's working now.