Unity cannot read string variable from PlayerController
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
c# unity3d
|
show 5 more comments
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
c# unity3d
I have doubt, that youStart()method insidePlayerControllernot firing in second scene.
– SeM
Nov 22 '18 at 12:00
Yes theStart()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 Power
Nov 22 '18 at 12:10
Did this linethePlayer = FindObjectOfType <PlayerController>();work or isthePlayetnull?
– derHugo
Nov 22 '18 at 17:54
YesthePlayer = FindObjectOfType <PlayerController>();works. I triedDebug.Log(thePlayer.name);which works fine
– Keith Power
Nov 22 '18 at 17:57
And what is the value ofportalName? What output would you expect exactly?
– derHugo
Nov 22 '18 at 18:05
|
show 5 more comments
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
c# unity3d
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
c# unity3d
c# unity3d
edited Nov 22 '18 at 18:43
Keith Power
asked Nov 22 '18 at 11:52
Keith PowerKeith Power
5,200164996
5,200164996
I have doubt, that youStart()method insidePlayerControllernot firing in second scene.
– SeM
Nov 22 '18 at 12:00
Yes theStart()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 Power
Nov 22 '18 at 12:10
Did this linethePlayer = FindObjectOfType <PlayerController>();work or isthePlayetnull?
– derHugo
Nov 22 '18 at 17:54
YesthePlayer = FindObjectOfType <PlayerController>();works. I triedDebug.Log(thePlayer.name);which works fine
– Keith Power
Nov 22 '18 at 17:57
And what is the value ofportalName? What output would you expect exactly?
– derHugo
Nov 22 '18 at 18:05
|
show 5 more comments
I have doubt, that youStart()method insidePlayerControllernot firing in second scene.
– SeM
Nov 22 '18 at 12:00
Yes theStart()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 Power
Nov 22 '18 at 12:10
Did this linethePlayer = FindObjectOfType <PlayerController>();work or isthePlayetnull?
– derHugo
Nov 22 '18 at 17:54
YesthePlayer = FindObjectOfType <PlayerController>();works. I triedDebug.Log(thePlayer.name);which works fine
– Keith Power
Nov 22 '18 at 17:57
And what is the value ofportalName? What output would you expect exactly?
– derHugo
Nov 22 '18 at 18:05
I have doubt, that you
Start() method inside PlayerController not firing in second scene.– SeM
Nov 22 '18 at 12:00
I have doubt, that you
Start() method inside PlayerController not firing in second scene.– SeM
Nov 22 '18 at 12:00
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
Nov 22 '18 at 12:10
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
Nov 22 '18 at 12:10
Did this line
thePlayer = FindObjectOfType <PlayerController>(); work or is thePlayet null?– derHugo
Nov 22 '18 at 17:54
Did this line
thePlayer = FindObjectOfType <PlayerController>(); work or is thePlayet null?– derHugo
Nov 22 '18 at 17:54
Yes
thePlayer = FindObjectOfType <PlayerController>(); works. I tried Debug.Log(thePlayer.name); which works fine– Keith Power
Nov 22 '18 at 17:57
Yes
thePlayer = FindObjectOfType <PlayerController>(); works. I tried Debug.Log(thePlayer.name); which works fine– Keith Power
Nov 22 '18 at 17:57
And what is the value of
portalName? What output would you expect exactly?– derHugo
Nov 22 '18 at 18:05
And what is the value of
portalName? What output would you expect exactly?– derHugo
Nov 22 '18 at 18:05
|
show 5 more comments
1 Answer
1
active
oldest
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);
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430434%2funity-cannot-read-string-variable-from-playercontroller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
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);
}
}
add a comment |
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);
}
}
add a comment |
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);
}
}
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);
}
}
answered Nov 23 '18 at 9:01
ShwetaShweta
1468
1468
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430434%2funity-cannot-read-string-variable-from-playercontroller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I have doubt, that you
Start()method insidePlayerControllernot firing in second scene.– SeM
Nov 22 '18 at 12:00
Yes the
Start()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 Power
Nov 22 '18 at 12:10
Did this line
thePlayer = FindObjectOfType <PlayerController>();work or isthePlayetnull?– derHugo
Nov 22 '18 at 17:54
Yes
thePlayer = FindObjectOfType <PlayerController>();works. I triedDebug.Log(thePlayer.name);which works fine– Keith Power
Nov 22 '18 at 17:57
And what is the value of
portalName? What output would you expect exactly?– derHugo
Nov 22 '18 at 18:05