Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by xCrusade · Feb 22, 2014 at 03:49 AM · javascriptcollisiontrigger

Scoring Issue

I am making a simple platform game. The score system I implemented doesn't work the way I expected and I've been digging through similar questions and all over the internet but cannot resolve my issue. The problem I am having is that when my character jumps on a platform, he is to be awarded a point. I've set up a GameMaster script with a global variable to hold the score across all my objects, I've set up trigger boxes for my character to collide with using the OnTriggerEnter button. This works and updates a test variable in the game master script as it should, incrementing the score each collision. However.. whenever I play the game, my score doesn't change in real time.. after I cancel the game, the new score will update in the game scene but not while I am actually playing which.. isn't desired. Here is the code for this section of the game.

My score script attached to each platform trigger: var scoreText : GUIText; //applying 3d text from unity to here will allow manipulation //private var alreadyScored : boolean = false; //already scored on current platform

 function Start () {
     scoreText.text = "Score: 0"; //append score value to score text, applies to game scene object
 }
 
 function OnTriggerEnter(player : Collider) { //when something enters trigger space (checkpoint
     if(player.tag == "Player" ) {    //if that something is the player object
         GameMaster.currentScore += 1;
         scoreText.text = "Score: " + GameMaster.currentScore; //apply new score
         Debug.Log("Scored a point!");
                 
     }
 }
 

My game master script:

 static var currentScore : int = 0; //global accessible script
 var score = 0;
 
 function Update () {
     score = currentScore;
 }
Comment
Add comment · Show 3
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 POLYGAMe · Feb 22, 2014 at 06:46 AM 0
Share

That looks like it should work, but I'd be tempted to handle the score stuff in the master script... simply use the collision to increment the score, keep the GUI object updating in realtime.

avatar image xCrusade · Feb 23, 2014 at 04:28 PM 1
Share

Thank you! I've transferred the score handling to the master script as suggested and moved the actually updating of the GUI object to the update method here, it works perfectly.

Here is the code for reference:

 static var currentScore : int = 0;                 //global accessible script
 var score = 0;                                     //test score
 var scoreText : GUIText;                         //applying 3d text from unity to here will allow manipulation
 //private var alreadyScored : boolean = false;     //already scored on current platform
 
 
 function Start () {
     scoreText.text = "Score: 0";                 //starting text
 }
 
 function Update () {
     score = currentScore;                         //test
     scoreText.text = "Score: " + currentScore;     //apply new score    
 }
 
 function OnTriggerEnter(player : Collider) {     //when something enters trigger space (checkpoint
     if(player.tag == "Player" ) {                //if player object collides
         currentScore += 1;                         //increase score by 1
         Debug.Log("Scored a point!");
                 
     }
 }
avatar image POLYGAMe · Feb 23, 2014 at 04:41 PM 0
Share

Awesome :)

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Having problems with 2d collision triggers (javascript) 1 Answer

Trigger Enter and Exit not working properly? 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

OnTriggerStay2D with many colliders 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