• 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
2
Question by chearner · May 13, 2010 at 05:02 AM · scoreguitextsingletoncs0308

Singleton score keeping and OnGUI updates

I am using the angry ant singleton example to update a score.

When I hit an instantiated object clone (e.g. balloon) I update the score from it's script...

MySingleton.Instance.Score += 5;

...then I destroy (e.g. pop) the object.

I have a GUIText on the hierarchy that displays the score. I have this script attached to it.

void OnGUI() { guiText.text = MySingleton.Instance.Score.ToString(); }

Works fine and displays my score without any nasty component referencing or attaching objects that could break later. Seems to me the cleanest method of keeping track of a score but I was worried that OnGUI() might be constantly updating that score even though the score doesn't change often. Is this using unnecessary processing to do this? Would it be better to somehow reference the guitext and update it only when the balloon pops and the score changes? Problem to me with that is you have to use getcomponent or someway of contacting the guitext script from my cloned objects (balloons) to update it.

Basically, should I update the score via OnGUI or call a method (e.g. UpdateScore) directly from the balloon object? If the latter, what is the best way of referencing the GUIText's script from an instantiated object.

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
3
Best Answer

Answer by Eric5h5 · May 13, 2010 at 05:19 AM

OnGUI does in fact update every frame. You generally wouldn't attach the OnGUI script to a GUIText object, since OnGUI is totally script-based and doesn't depend on that. You can use an empty game object instead, and the script would contain this:

public Rect scoreRect;

void OnGUI { GUI.Label(scoreRect, MySingleton.Instance.Score.ToString()); }

A GUIText object is completely different and only updates when the text is changed, so it can be faster, although the actual performance difference isn't likely to be measurable in most cases. You can make a function in a singleton and attach it to the GUIText:

public void UpdateScore (int addToScore) {
    score += addToScore;
    guiText.text = score.ToString();
}

Then you'd call MySingleton.instance.UpdateScore (100); if you wanted to add 100 points to the score and have the text updated.

Comment
Add comment · Show 7 · 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 chearner · May 13, 2010 at 05:52 AM 0
Share

I actually have the guitext on a empty gameobject with a script attached to it (OnGUI). I made that a prefab and dragged it to my hierarchy to display my score. Are you suggesting I add the UpdateScore function to $$anonymous$$ySingleton class (that isnt attached to anything currently) and attach that to my prefab ins$$anonymous$$d, replacing my current script?

avatar image Eric5h5 · May 13, 2010 at 06:03 AM 0
Share

@chearner: You shouldn't be using GUIText at all if you're using OnGUI. If you have a GUIText component, then the game object isn't empty. You should get rid of it and use GUI.x functions ins$$anonymous$$d.

But if you don't want to use OnGUI, then you can use a GUIText object with a script containing the UpdateScore function (or something like it) attached to the GUIText object. It doesn't have to be (and probably shouldn't be) the $$anonymous$$ySingleton class you already have, but can be made into a singleton in the same way for easy access.

avatar image chearner · May 14, 2010 at 05:34 AM 0
Share

@eric5h5: Sorry, I'm still struggling to wrap my head around using singletons. I created a singleton script like the component-based example on the angry mant singleton wiki page. I also added the UpdateScore function to it you suggested. I then attached this script to a GUIText object in my hierarchy. I get an error saying the non-generic method 'AddComponent' cannot be used with type arguments. code: instance = new GameObject("ScoreController").AddComponent(); Is this a good way to create and use a singleton to manage my score?

avatar image Eric5h5 · May 14, 2010 at 06:47 AM 0
Share

@chearner: You can use "instance = this;". See here: http://www.unifycommunity.com/wiki/index.php?title=Singleton

avatar image chearner · May 16, 2010 at 05:10 AM 1
Share

The best way I found to solve it was create the singleton using the non-component method from the wiki. To that I add the score keeping related methods including public void UpdateScore(int addToScore) { score += addToScore; GameObject scoreObject = GameObject.Find("GUI Score"); scoreObject.guiText.text = score.ToString(); } I never attach the script to any object, I use Find() to locate my GUIText object. To me it seem the cleaner and I don't have to use OnGUI().

Show more comments

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How would I make the text bigger on this script? 1 Answer

Show score when enemy is destroyed C# 1 Answer

I used a GUIText to show the score in my game but it doesn't show my true results 0 Answers

What is the best way to show (Instantiate) a GUIText at the position of a GameObject? 3 Answers

Picking up an Item and telling you how many you have 1 Answer


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