• 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
0
Question by Beta_Artist · Sep 22, 2011 at 01:52 AM · scorecounter

Trying to get score counter...

I got my score counter to show up on my GUI Text, yet when I switch to another object with the same script its resets the score to zero. What can i change or add to stop it from resetting?

 var speed = 5;
 var buck = 100;
 var deer = 10;
 private var score : int = 0;
 var ScoreBoard: GUIText;
 
 
 function Update () {
     transform.position += Vector3.forward * Time.deltaTime * speed;
 }
 
 function OnMouseEnter () {
     Debug.Log("Hit");
     speed = 15;
     deer += 10;
     ScoreBoard.text = ("Score: ") + deer;
 }    
 
 function OnMouseExit () {
     Debug.Log("Safe");
     speed = 5;
 }
Comment
Add comment · Show 1
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 syclamoth · Sep 22, 2011 at 01:58 AM 0
Share

What do you mean, 'switch to another object'? I wasn't aware that moving components around was exactly straightforward. If you mean putting another instance of the script on a different object, then of course the score will reset to zero- it's a private member of a different object!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Holynub · Sep 22, 2011 at 02:18 AM

Wait. Are you attaching the same script to multiple objects? Because if you're doing that, this will treat each 'val deer' as different ones.

You'd need to setup a control script that acts as a global counter. This controller script can handle things like score keeping.

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
avatar image
0

Answer by aldonaletto · Sep 22, 2011 at 02:12 AM

You can declare score as a static variable:

 static var score : int = 0;

A static variable is created once at the program beginning, lives while the program is running and is unique: all script instances use the same variable. Non-static variables exist in each script instance and are independent of each other.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

what is the problem of this code ? 2 Answers

unknown identifer explosion problem 2 Answers

help with score display concept 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Connecting 2 scripts(javascpt)? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges