- Home /
How to reference a variable between scenes and scripts in c#
I'm trying display the score at the end of a simple game on a game over scene. I have two scripts: GameManager and GameOverManager, in GameManager, public Text score
displays the score. I want to display that text on my GameOver scene, as public Text EndScore
. All I want to do is set EndScore equal to score in my GameOverManager script so I can display it.
You can't have cross scene references of GameObjects. Depending on how you are loading the game over scene, either you can keep the gameobject alive across scenes by using DontDestroyOnLoad(), or you can wrap the script in a singleton model so that it can be accessed in the next scene.
Answer by pcdrive · Oct 09, 2016 at 12:54 PM
Hi dude!
AFAIK the only way you can do this is to create a gameObject attach the data you need to it and dont let it destroyed when load new scene by the Object.DontDestroyOnLoad function.
This way you will have a gameObject wich will stay when you switch scenes, so you can load data from and into it.
I hope i understanded your problem the right way, so i could help =)
Good luck finding the solution
Your answer
![](https://koobas.hobune.stream/wayback/20220612094258im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Collision Script Not Working 1 Answer