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
Start()method insidePlayerControllernot firing in second scene. - SᴇMStart()method does not fire in the second scene because the player is not destroyed. I set a new value for thestartPointbefore I load the new scene in theOnTriggerEnter2Dwhich does work. However when the new PortalController in the second scene loads it cannot read the value even though it is there - Keith PowerthePlayer = FindObjectOfType <PlayerController>();work or isthePlayetnull? - derHugothePlayer = FindObjectOfType <PlayerController>();works. I triedDebug.Log(thePlayer.name);which works fine - Keith PowerportalName? What output would you expect exactly? - derHugo