• 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 fjcym · Mar 28, 2012 at 06:28 AM · score

Score Counter

I have a JavaScript and i want to add a counter to my game. I have a script added to my Character but it doesn't count the score. How do I do fix this?

JavaScript:

 var score = 0;
 var scoreText = "Score: 0";
 var mySkin : GUISkin;
 
 function OnTriggerEnter( other : Collider ) {
     Debug.Log("OnTriggerEnter() was called");
     if (other.tag == "point") {
         Debug.Log("Other object is a point");
         score += 1;
         scoreText = "Score: " + score;
         Debug.Log("Score is now " + score);
         Destroy(other.gameObject);
     }
 }


Counter:

 var MenuSkin : GUISkin;
 var toggleTxt : boolean;
 var toolbarInt : int = 0;
 
 function OnGUI() {
     GUI.skin = MenuSkin;
     GUI.BeginGroup(new Rect(Screen.width/2-150,Screen.height/100-0,250,50));
         GUI.Box(Rect(0,0,250,50),"Score: ");
     GUI.EndGroup ();
 }
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
0

Answer by Berenger · Mar 28, 2012 at 06:56 AM

So for some reason, you don't want to merge those two scripts. I guess you they are on two different objects.

You need a reference of the instance of the first script into the second one to access it's score variable. Declare the var at the beginning of the Counter script, then drag-drop the gameObject containing the first script onto that variable in the inspector of the second object.

Comment
Add comment · Show 2 · 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 fjcym · Mar 28, 2012 at 03:53 PM 1
Share

Could you post the script put together because I'm a but lost.

avatar image Bunny83 · Mar 28, 2012 at 03:56 PM 0
Share

@fjcym: Use comments!!! I've converted your "answer" to a comment.

Also don't ask other people to write code for you. If you need a programmer, search for one on the collaboration forum

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Don't destroy script on load 2 Answers

Money System Not Working Please Help!!!! (Code fully commented!) 3 Answers

When should I save data 2 Answers

Does any know how to hide and show 2d sprites in unity 5 by using a 2d boxcollider set to trigger when player collides with the boxcollider 1 Answer

After I initially add score it keeps adding. 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