• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by carter-carl30 · Jun 17, 2012 at 09:36 PM · score

score script, x2 for 10 seconds

Hi all, I have this score script:

 function OnTriggerEnter(enterer : Collider) 
 {
     if (enterer.collider.gameObject.CompareTag("Bullet"))
     {
         Debug.Log("Score!");
         Scorecounter.Counter ++;
     }
 }

and this score counter script:

 static var Counter : int = 0;
 
 function Update () 
 {    
     guiText.text = "Score: " + Counter;
 }  

what I would like to do is have a collectable item, and when the player collides with it, it increases the score x2 (eg, if you usually get 10pts for breaking a block you would get 20pts) and this happens for a 10 second period after collecting the item then scoring resets back to normal.

I was thinking if I put a function OnTriggerEnter(enterer : Collider) { if (enterer.collider.gameObject.CompareTag("item")) {

and then somehow make the score go up x2. I don't know what to add to my score script to do this any pointers?

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

Answer by Berenger · Jun 17, 2012 at 09:50 PM

You need a a var similar to your counter which multiply the reward, 1 in your case. Something like Scorecounter.Counter += 1 * Scorecounter.Multiplicator;

Then, you can increase that multiplicator or decrease it. You'll need Invoke or a coroutine for the 10 seconde delay. To make seems clearer :

score script : The GUI part

 static var Counter : int = 0;
 static var Multiplicator : int = 1;
 
 function Update () 
 {    
     guiText.text = "Score: " + Counter;
 }

brick script : The score is increased

 private const var BASE_REWARD  : int = 1; // That way you know what it represent.

 function OnTriggerEnter( enterer : Collider ) 
 {
     if( enterer.collider.gameObject.CompareTag("Bullet") )
     {
         Debug.Log("Score!");
         // This could also be a static function.
         Scorecounter.Counter += BASE_REWARD * Scorecounter.Multiplicator;
     }
 }

powerup script : Make every brick give +2 instead of +1

 function OnTriggerExit( enterer : Collider ) 
 {
     //this is the powerup hitting player
     // Are you sure the player has the tag "scoremultiplyer_powerup" ?
     if (enterer.collider.gameObject.CompareTag("scoremultiplyer_powerup"))
     {
         Scorecounter.Multiplicator++;
         // Note that if this gameObject is destroyed before those 10 seconds,
         // the function won't be called.
         Invoke( "DecreaseInXSeconds", 10.0 );
     }
 }
 
 function DecreaseInXSeconds(){
     // Make sure we don't have a null multiplicator.
     Scorecounter.Multiplicator = Mathf.Max( 1, Scorecounter.Multiplicator-1 );
 }
Comment
Add comment · Show 10 · 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 carter-carl30 · Jun 18, 2012 at 05:54 PM 0
Share

@Bérenger $$anonymous$$antoue, thanks for your response. I am trying (i'm still very much a newbie at scripting) to put something together. I have this script below that I will put onto the player.

I am getting the error "Assets/Standard Assets/Scripts/score_x2_powerup.js(9,42): BCE0019: '$$anonymous$$ultiplicator' is not a member of 'Scorecounter'."

I think this is because $$anonymous$$ultiplicator is not referenced in my Scorecounter script but I don't know what to put into it? would I add a var $$anonymous$$ultiplicator : int=0; to my Scorecounter script and then guiText = "Score: " + Counter + $$anonymous$$uliplicator; ?

 #pragma strict
 var $$anonymous$$ultiplicator : int = 0;
 
 function OnTriggerExit(enterer : Collider) 
 {
     //this is the powerup hitting player
     if (enterer.collider.gameObject.CompareTag("scoremultiplyer_powerup"))
     {
 
 Scorecounter.Counter += 1 * Scorecounter.$$anonymous$$ultiplicator;
 
 }
 }
avatar image Berenger · Jun 18, 2012 at 07:06 PM 1
Share

Nop, you need to understand what is a static var. It's a var that don't need an instance of an object, is unique and that exist as long as the program is running. So, in ScoreCounter.js :

 static var Counter : int = 0;
 static var $$anonymous$$ultiplicator : int = 1;
avatar image carter-carl30 · Jun 18, 2012 at 11:04 PM 0
Share

Thankyou for explaining that, I have that working now, so when I collect the item the score goes up with the multiplicator.

at the moment I score by hitting bricks (which have a trigger attached to) that has the scorescript attached and gives +1 point per brick hit.

How would I make collecting the scoremultiplyer_powerup affect those? or is that really complicated?

avatar image Berenger · Jun 18, 2012 at 11:11 PM 0
Share

If the score script is using Scorecounter.$$anonymous$$ultiplicator, and if the scoremultiplyer_powerup does increase that multiplicator, nothing else to do !

avatar image Berenger · Jun 18, 2012 at 11:39 PM 1
Share

Oh all right, I get it now. You don't want further points to be doubled, you want the current score to be doubled. Well, Scorecounter.Counter *= 2 should do just that, doesn't it ?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

INCREASE SCORE MORE AND MORE? 1 Answer

How to go about purchasing objects with player score/points? 1 Answer

Multiplayer ScoreBoard not Updating Correctly 0 Answers

Where am I going wrong with these scripts? 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