• 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 Icarus370 · Mar 09, 2016 at 01:32 AM · singleton

help sending non static variable to a singleton

Im trying to access a non static variable score with the code below. using UnityEngine; using System.Collections;

 public class Score : MonoBehaviour { 
     
     public int score = 0;
     
     void Start()
     {
 
     }
     
     void Update()
     {
         ShowScore();
     }
 
     void ShowScore()
     {
         string timeString;
         
         timeString = score.ToString();
         GetComponent<GUIText>().text = timeString;
     }
     
 }

and give the information to the singleton.

public static class Leaderboard { public const int EntryCount = 10;

 public struct ScoreEntry {
     public string name;
     public int score;
     
     public ScoreEntry(string name, int score) {
         this.name = name;
         this.score = score;
     }
 }
 
 private static List<ScoreEntry> s_Entries;
 
 private static List<ScoreEntry> Entries {
     get {
         if (s_Entries == null) {
             s_Entries = new List<ScoreEntry>();
             LoadScores();
         }
         return s_Entries;
     }
 }
 
 private const string PlayerPrefsBaseKey = "leaderboard";
 
 private static void SortScores() {
     s_Entries.Sort((a, b) => b.score.CompareTo(a.score));
 }
 
 private static void LoadScores() {
     s_Entries.Clear();
     
     for (int i = 0; i < EntryCount; ++i) {
         ScoreEntry entry;
         entry.name = PlayerPrefs.GetString(PlayerPrefsBaseKey + "[" + i + "].name", "");
         entry.score = PlayerPrefs.GetInt(PlayerPrefsBaseKey + "[" + i + "].score", 0);
         s_Entries.Add(entry);
     }
     
     SortScores();
 }
 
 private static void SaveScores() {
     for (int i = 0; i < EntryCount; ++i) {
         var entry = s_Entries[i];
         PlayerPrefs.SetString(PlayerPrefsBaseKey + "[" + i + "].name", entry.name);
         PlayerPrefs.SetInt(PlayerPrefsBaseKey + "[" + i + "].score", entry.score);
     }
 }
 
 public static ScoreEntry GetEntry(int index) {
     return Entries[index];
 }
 
 public static void Record(string name, int score) {
     Entries.Add(new ScoreEntry(name, score));
     SortScores();
     Entries.RemoveAt(Entries.Count - 1);
     SaveScores();
 }

}

Comment
Add comment · Show 2
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 NoseKills · Mar 09, 2016 at 09:44 AM 0
Share

What is the question? What have you tried? How did that not work?

avatar image Icarus370 NoseKills · Mar 13, 2016 at 07:51 PM 0
Share

I want to send the contents of score in the Score class to the $$anonymous$$erboard class score so it will display the score correctly from a different script ( not shown ) I have tried creating an instance of the class within the leaderboard but that does not work.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Icarus370 · Mar 15, 2016 at 07:11 PM

never mind, i actually figured it out. had to change the score variable in the score class to static. simple fix which i overlooked

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

49 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

Related Questions

Alternative to singletons 0 Answers

Singleton working in editor, but not in build 0 Answers

if (gameobject != null) giving UnassignedReferenceException 2 Answers

Trying to make "Gallery Unlocker" script with Singleton 1 Answer

Using Grid-like singletons but have an issue with Coroutines. 2 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