Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by juhainamtc · Dec 19, 2018 at 04:18 AM · playerprefsvariablesswitch scenes

How to pass more than one variable between scenes using playerprefs

Hello,

I have 2 scenes. Scene1 has two texboxes, to display the score and video title. I am storing the text of these two in two different variables. I tried passing on the score to the next scene using playerprefs and it works, same code when I am using with different directory name for the other variable i get the following error in console: NullReferenceException: Object reference not set to an instance of an object Scorecontrol.Start () (at Assets/Scripts/Scorecontrol.cs:15)

I am basically passing on the score and the video title along with other details filled in the by the user on the Scene 2 as an email. Is it possible to use playerpref to pass on more than one variable between scenes.I dont have much idea about playerprefs.

here's my code for saving the values of the variables in scene 1:

PlayerPrefs.SetString("CurrentScore", scoreText.text); PlayerPrefs.SetString("VideoTitle", Vidtitle.text);

and this is for getting the values in the Scene2:

scoretext2.text = PlayerPrefs.GetString("CurrentScore"); vtitle.text = PlayerPrefs.GetString("VideoTitle");

I get the error for the second line.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by Cornelis-de-Jager · Dec 19, 2018 at 05:06 AM

The easiest and best way I found to do this is to have a object in the scene that doesn't get destroyed when switching between scenes. Like a GameManager object.

Simply do:

 void Awake () {
      DontDestroyOnLoad(this.gameObject);
 }

Then you can simple add the variables you want to be saved to it.

 public int MyInt;
 public string MyString";
 ...

I like this method because you can add entire scripts to the object. So you can have scripts for different things:

 PlayerDetails.cs
 Achievements.cs
 UIController.cs
 EnemeySpawner.cs
 ...

And you are not limited to variables. Common functionality used between scenes can also be used.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image juhainamtc · Dec 19, 2018 at 05:18 AM 0
Share

Hi @Cornelis-de-Jager thanks for your answer, is there a more detailed section explaining how to do this please? how does Void awake() work? Any idea why my method isnt working>? $$anonymous$$y score is also a text actually, it will be displayed as 4/5 thats why I have saved it as a tring variable. The thing is I tried to place the code:

scoretext2.text = PlayerPrefs.GetString("CurrentScore");

     vtitle.text = PlayerPrefs.GetString("VideoTit");


in opposite order, that is GetString("VidTit") first , then playerprefs doesnt retrive the values for both the variables!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

103 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

PlayerPrefs string variables 1 Answer

Persisting Audio Mixer Exposed Variables 0 Answers

How can you save a long variable with Playerprefs 0 Answers

Convert class of int vars into an array 0 Answers

Is it safe to save values in a prefab instead of using PlayerPrefs? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges