0
votes

I have a public string startPoint set in my PlayerController. I am accessing this string from another script PortalController and I am setting and reading it which works for the most part.

My problem is:

I read the Debug.Log(thePlayer.startPoint); when the first start scene loads from the PortalController I can set the startPoint and load the second scene and I can confirm from the Unity panel that it is indeed set for the PlayerController in the second scene. However I cannot read the Debug.Log(thePlayer.startPoint); from the second scene. It come out blank and has a length of 0. No errors.

PlayerController

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

public class PlayerController : MonoBehaviour {

    float speed = 2f;
    Vector2 targetPos;

    private Rigidbody2D myRigidbody;
    private Animator myAnim;

    private static bool playerExists;

    public string startPoint;

    private void Start()
    {
        startPoint = "startValue";

        myRigidbody = GetComponent<Rigidbody2D>();
        myAnim = GetComponent<Animator>();

        if(!playerExists){
            playerExists = true;
            DontDestroyOnLoad(transform.gameObject);
        } else {
            Destroy(gameObject);
        }

        targetPos = transform.position;

    }

PortalController

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


public class PortalController : MonoBehaviour {

    [SerializeField]
    private string sceneToLoad;

    public string portalName;
    public string spawnPortal;

    private PlayerController thePlayer;
    private CameraController theCamera;

    // Use this for initialization
    void Start () {

        thePlayer = FindObjectOfType <PlayerController>();

        //works in the first scene but not in the second
        Debug.Log(thePlayer.startPoint);

        if (thePlayer.startPoint == portalName)
        {
            thePlayer.transform.position = transform.position;
            //Debug.Log(thePlayer.startPoint);
        }

    }

    // Update is called once per frame
    void Update () {

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.name == "Player"){
            thePlayer.startPoint = spawnPortal;
            SceneManager.LoadScene(sceneToLoad);
        }
    }
}

The Player object does not get destroyed between scenes and a new portal gameObject get created. It seems like that when the new portal gameObject with the PortalController get created in scene two it cannot access the string startPoint in the PlayerController the PortalController in scene in could have.

If I load scene two directly Debug.Log(thePlayer.startPoint); works fine. It only stops switching

1
I have doubt, that you Start() method inside PlayerController not firing in second scene. - SᴇM
Yes the Start() method does not fire in the second scene because the player is not destroyed. I set a new value for the startPoint before I load the new scene in the OnTriggerEnter2D which does work. However when the new PortalController in the second scene loads it cannot read the value even though it is there - Keith Power
Did this line thePlayer = FindObjectOfType <PlayerController>(); work or is thePlayet null? - derHugo
Yes thePlayer = FindObjectOfType <PlayerController>(); works. I tried Debug.Log(thePlayer.name); which works fine - Keith Power
And what is the value of portalName? What output would you expect exactly? - derHugo

1 Answers

0
votes

I just tried your code with PlayerController and PortalController in 1st scene and Only PortalController in 2nd scene and the output is what you are expecting, no errors and getting startPoint value perfectly.

But make sure on start of 2nd scene player isn't colliding with portalController or else it will call triggerEnter another time and set value of SpawnPortal of 2nd scene's PortalController which can be blank and that's why you are getting this issue.

You can check that by debug in triggerEnter..

private void OnTriggerEnter2D(Collider2D collision)
{
    Debug.Log("OnTriggerEnter " + collision.gameObject.name);
    if (collision.gameObject.name == "Player")
    {
        thePlayer.startPoint = spawnPortal;
        SceneManager.LoadScene(sceneToLoad);
    }
}