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 /
  • Help Room /
This question was closed Jan 11, 2016 at 08:28 AM by Le-Pampelmuse for the following reason:

Duplicate Question. Example: http://answers.unity3d.com/questions/36738/how-can-i-pass-data-between-two-levels.html

avatar image
0
Question by triggerdog · Jan 11, 2016 at 07:58 AM · c#static

How do I save Entered Name and show it for the rest of the game?

absolute Unity beginner here. In a text game:

Start Screen says: "What is your name?" and there will be an InputField for Player to enter name.

How do I save that name and show it in the next scene? My InputName.class in C#:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
  
 public class InputName : MonoBehaviour {
     public static string username = "";
    
    
     [SerializeField]
     InputField input;
    
     [SerializeField]
     public Text NameDisplay;
     public Text StartGame;
    
     public void GetInput(string username){
         Debug.Log ("Entered : "+username);
         input.text= " ";
         //username = input;
         NameDisplay.text = ("Hello, " +username.ToString()+"!");
         PlayerPrefs.GetString ("Username", username);
         Debug.Log (username);
         input.enabled = false;
        
         DontDestroyOnLoad(this)
         //Application.LoadLevel("Game2");
     }
  
     // Use this for initialization
     //void Start () {
         //NameDisplay.text = username;
     //} */
    
     //Update is called once per frame
     void Update(){
         if (input.enabled == false){
             LetsStartGame();       
             }
     }
     void LetsStartGame(){
         StartGame.text = "Press Space to Continue";
         if (Input.GetKeyDown(KeyCode.Space))
         Application.LoadLevel("Game2");
         }
     }

Once Player hits KeyCode.Space, Game2 will load. I would like to transfer the value from username variable into Game2 Scene. I tried the following code making username variable a static:

  public class PlayerName : MonoBehaviour {
 
  string usernameFromInputName;
 //public class _inputName_inputName{
 //public static string username;
   
 //[SerializeField]
 public Text text;
  
 // Use this for initialization
 void Start () {
      //text.text= InputName.username;
      
 }
   
 // Update is called once per frame
 void Update () {
     text.text = InputName.username;
     usernameFromInputName = InputName.username;
     //print (InputName.username);
     Debug.Log(InputName.username);
     //PlayerPrefs.GetString (inputName.username);
 }

}

There are no compiler errors but when Player enters their Name, their Name is not carried over to the next scene.

Hope my question makes sense and any help is appreciated.

Comment
Add comment · Show 3
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 Le-Pampelmuse · Jan 11, 2016 at 08:27 AM 2
Share

Hi. You don't have to use [Help] on Unity Answers. It's obvious you need help when you ask a question. :)

It's a good habit to google your questions before asking new ones. That way we can avoid having hundreds of duplicate questions:

alt text Additionally it saves you the time to write questions. ;)

Have a look at "Persistence" in Unity.

using DontDestroyOnLoad() is one option you can quickly become used to.

unbenannt.png (68.3 kB)
avatar image triggerdog Le-Pampelmuse · Jan 14, 2016 at 08:06 AM 0
Share

Hi. Sorry for the duplicate question and thanks for your help. I did google but I got JavaScript tutorials or they use PlayerPrefs to save data (which I want to avoid).

As for the DontDestroyOnLoad(), I did try that. There were no compiler errors produced but the username's value from the InputName class did not transfer to the next scene.

Based on the videos I've watched, including the one you referred, I may need to rewrite a good chunk of this script?

(I'm not using multiple scenes, just one scene to another.)

I agree on you in that I should google and I will continue to keep searching. :)

If you can help me further on this, that would be great. If not, that's ok.

avatar image Le-Pampelmuse triggerdog · Jan 15, 2016 at 03:20 AM 3
Share

(I'm not using multiple scenes, just one scene to another.)

If you are loading the same scene multiple times, that is still using multiple scenes because loading a new scene erases everything changed in the current scene, just like the play mode in the editor.

If the entire game takes place in one scene, then you are not using multiple scenes, which erases the need for persistent things.

DontDestroyOnLoad() helps with that.

The one thing to note about it, is: When you have the same gameobject with the same scripts in the next scene, you will have to have it check for any duplicate of itself and disables itself so there are no duplicates prior to executing.

But that's covered in almost every DontDestroyOnLoad() related tutorial/post.

It's not neccessary to use PlayerPrefs, because you don't quit the application between setting the value and getting it.

Regarding your code, you might want to place DontDestroyOnLoad() in an early function like Start() or Awake().

PS: It's not really your fault for posting this. It's the fault of the moderator who chose to publish it. But, you should already have known the fundamental forum rules. ;)

Check out the FAQ and the User Guide in case you didn't.

You may have already found the solution by googling. I kind of late.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

StartCoroutine in a static member 0 Answers

Static bool 1 Answer

Switch-Case Statements and stateInfo.fullPathHash 1 Answer

When should I use static fields/methods? 2 Answers

Can't destroy GameObject in static method 3 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