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 Feb 15, 2018 at 01:36 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by plumel · Mar 16, 2016 at 05:42 PM · guifileonclickiowrite data

Not writing to file correctly

Hi, I want the code to write PlayerNameFinal and count to an array in leaderboard.txt when SaveScoreButton is pressed.

This is currently what is being written to the file: System.Collections.Generic.Dictionary`2[System.String,System.Int32]

Code:

using UnityEngine; using System.Collections; using System.Collections.Generic; using System.IO; using Vexe.FastSave; using Vexe.Runtime.Types;

public class GUI_Button : MonoBehaviour {

 public PlayerMovement GUIScript;
 public RandomSpawn GUIScript1;
 private int count = RandomSpawn.count;
 public string PlayerName = "Enter name";
 public string fileName = "leaderboard.txt";
 string[] PlayerAndScore = new string [1];
 public string PlayerNameFinal;
 public string count1 = "";
 private bool SaveScoreButton = false;

 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);
         SaveScoreButton = GUI.Button (new Rect (400, 500, 200, 100), "Save Score");
         CountToString ();
     }
 }
 void OnClick()
 {
     PlayerNameFinal = PlayerName;
 }
 void Start()
 {
     PlayerAndScore [0] = PlayerNameFinal;
     PlayerAndScore [1] = count1;
     System.IO.File.WriteAllLines(@"Desktop\Pixel Ninjav3.3\Assets\leaderboard.txt", PlayerAndScore);
 }
 void CountToString ()
 {
     count1 = count.ToString();
 }

}

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

  • Sort: 
avatar image
1
Best Answer

Answer by Brocccoli · Mar 16, 2016 at 06:55 PM

This code is strange.

For one, you declare a string[] of length 1 at the top, and then you add 2 strings to the array. This code should throw an IndexOutOfBoundsException before it even gets to the IO call.

Perhaps try fixing the size of your PlayerAndScore array to:

 string[] PlayerAndScore = new string[2];

Although I'm not sure how much that will help you, as I'm totally boggled as to how you get an output in the first place.

Comment
Add comment · Show 20 · 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 plumel · Mar 16, 2016 at 07:39 PM 1
Share

It turns out I was looking at the wrong thing and nothing was output. I changed the size of the array, nothing else has happened ;_;

avatar image Brocccoli plumel · Mar 16, 2016 at 08:58 PM 0
Share

So you changed the size of the array, and now you get no output?

Can you set a breakpoint? Do you ever get to the IO call?

avatar image plumel Brocccoli · Mar 17, 2016 at 09:42 AM 0
Share

Where would you suggest putting the breakpoint? And IO call? Sorry but I really could do with some guidance on what to do.

Show more comments
avatar image Brocccoli plumel · Mar 17, 2016 at 01:17 PM 1
Share

Looking at it now, the breakpoint should definitely get hit as its in the Start() method (doh). Is the script attached to a GameObject? Either way, set a breakpoint on the System.IO.File.WriteAllLines(...) line and check the values of the string array when it gets hit. $$anonymous$$aybe the string array is filled with empty strings.

When I say IO call, I just meant the line you were using to write to the file.

avatar image plumel Brocccoli · Mar 17, 2016 at 05:57 PM 0
Share

Well... the break point.. This is what comes up alt text

Also, this freezes unity.

I tried putting in a Debug.LogError to the PlayerAndScore variable.. This is what it says before the GUI buttons come up and after typing something in

System.String[] UnityEngine.Debug:LogError(Object) GUI_Button:Start() (at Assets/Scripts/GUI_Button.cs:38)

breakpoint.png (17.9 kB)
Show more comments
Show more comments
avatar image Brocccoli plumel · Mar 17, 2016 at 06:36 PM 0
Share

You can't just print out "PlayerAndScore" that's just going to print out "Object" as you see.

When you hit the breakpoint, you should be able to mouse over PlayerAndScore and dig into the array to see the values that are stored.

avatar image plumel Brocccoli · Mar 17, 2016 at 06:44 PM 0
Share

So I've made the break point alt text

And I've run the game. Nothing is happening in monodevelop or the game

breakpoint2.png (14.0 kB)
Show more comments
avatar image plumel · Mar 16, 2016 at 07:40 PM 0
Share

Also, was more or less coding this in a blind rage. This is for my computer science coursework and I haven't the foggiest how im gonna get it done in time

avatar image plumel · Mar 17, 2016 at 09:09 PM 0
Share

@Brocccoli thank you for all your help. really.

I have the command set on the save game button. upon it being pressed, the array is written to the file.

THAN$$anonymous$$ YOU SO $$anonymous$$UCH

avatar image Brocccoli plumel · Mar 18, 2016 at 04:59 PM 0
Share

I'm very happy to hear you got it working.

Good luck in the future!

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

Instantiated button prefab causes Delegation error on click and does not add OnClick Listener 0 Answers

Is it possible to use a GUI Button (On Click) to force Input keyboard commands like spacebar for Jump? 2 Answers

can't seem to open binary file after game is built (inside asset folder) 2 Answers

is there a way to ignore a click when i press on a button? 1 Answer

Access to the path is denied 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