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 /
avatar image
0
Question by plumel · Mar 17, 2016 at 10:25 AM · guitextfield

Save whats in GUI.Text field to variable

Hi, I need to save what the player enters to the text field, and then write that to a file so I'm able to store the playername and score to the file and then sort them, so I'm able to read from the file and display the name with the score on a different scene.

Ideally the player would press a button "Save score" and then the playername would be written to the variable.

I'm trying to make a leaderboard but I'm finding it quite difficult right now.

Comment
Add comment · Show 5
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 hexagonius · Mar 17, 2016 at 10:46 AM 0
Share

new or old UI? what do you have so far?

avatar image plumel hexagonius · Mar 17, 2016 at 11:06 AM 0
Share

Pretty sure it's the new UI.

This is what I've done, It's messy.

 public Player$$anonymous$$ovement GUIScript;
  public RandomSpawn GUIScript1;
  private int count = RandomSpawn.count;
  public string PlayerName = "Enter name";
 5. public string fileName = "leaderboard.txt";
  string[] PlayerAndScore = new string [1];
  public string PlayerNameFinal;
  public string count1 = "";
  private bool SaveScoreButton = false;
 10.
  void OnGUI ()
  {
      if (Player$$anonymous$$ovement.livess <= 0) 
      {
 15.         GUI.Button (new Rect (400, 200, 300, 200), "Score:" + (count+10));
          PlayerName = GUI.TextField (new Rect (400, 400, 300, 100), PlayerName);
          SaveScoreButton = GUI.Button (new Rect (400, 500, 200, 100), "Save Score");
          CountToString ();
      }
 20. }
  void OnClick()
  {
      PlayerNameFinal = PlayerName;
  }
 25. void Start()
  {
      PlayerAndScore [0] = PlayerNameFinal;
      PlayerAndScore [1] = count1;
      System.IO.File.WriteAllLines(@"Desktop\Pixel Ninjav3.3\Assets\leaderboard.txt", PlayerAndScore);
 30. }
  void CountToString ()
  {
      count1 = count.ToString();
  }
 
avatar image plumel hexagonius · Mar 17, 2016 at 01:07 PM 0
Share

$$anonymous$$ake any sense to you @hexagonius ?

$$anonymous$$y original thoughts on this included me making a GUI text field which the player will be able to write to, this will then be saved to a variable. The player will press ‘Save game’ and the variable stored in the text field will be moved to a permanent variable. From here, I will put the name variable and score into an array, which will be stored in an array in text file. I will then make a script to sort the array in the text file and output the {0} and {1} values for the highest score, {2} and {3} for 2nd highest score etc.

avatar image hexagonius plumel · Mar 17, 2016 at 04:11 PM 1
Share

Using "void OnGUI()" is the old UI. The new UI uses real screen elements. SaveScoreButton is the status of the button when clicked. Since OnGUI is called multiple times per frame, you should execute what you want to do on button click right there ins$$anonymous$$d of saving the bool.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by plumel · Mar 17, 2016 at 06:13 PM

@hexagonius I think this might be it? But im getting an error (in orange) "Possible mistaken empty statement on line "if (GUI.Button (..." aka savescore button

     void OnGUI ()
     {
         if (PlayerMovement.livess <= 0) 
         {
             GUI.Button (new Rect (400, 200, 300, 200), "Score:" + (count+10));
             PlayerName = GUI.TextField (new Rect (400, 400, 300, 100), PlayerName);
             CountToString ();
             if (GUI.Button (new Rect (400, 500, 200, 100), "Save Score"));
             {
                 PlayerNameFinal = PlayerName;
                 PlayerAndScore [0] = PlayerNameFinal;
                 PlayerAndScore [1] = count1;
                 Debug.LogError (PlayerAndScore);
                 System.IO.File.WriteAllLines(@"leaderboard.txt", PlayerAndScore);

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 hexagonius · Mar 17, 2016 at 11:00 PM 1
Share

remove the ; at the end of the if statement

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

70 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

Related Questions

Are TextFields in GUI.window generally uneditable? 0 Answers

Working with the GUI 4.6 (textfields and buttons) 1 Answer

How to count down by mouse click? 1 Answer

Panel content size fitter not working 2 Answers

How to use individual textures as font in GUI? 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