• 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 8r3nd4n · Oct 30, 2011 at 03:35 AM · score

Score multiplier/ chaining system

Hi gang

I am working on a game that I would like to add a chaining system to my score where if a player hits an object of the same colour, it sequentially increases the multiplier, and if they hit an object of a different colour, the multiplier resets and begins with the new object as the starting base. So if hit red object, multiply x 2, next hit if red, multiply by 3, next hit if blue, reset multiplier to blue as the chain start. This script is attached to the projectile and the scoring all works fine except for the chaining does not work at all.

My code is very messy and I know there are better ways to achieve this but I'm stumped as to how:

 using UnityEngine;
 using System.Collections;

 public class Ball : MonoBehaviour {
 
 
 public GameObject explosionPrefabRed;
 public GameObject explosionPrefabBlue;
 public GameObject exp;
 public static float bonus;
 public int redCount;
 public int blueCount;
 public int chainBonus = 1;
 
 // Use this for initialization
 void Start () 
 {
 redWallCount = 1;
 blueWallCount = 1;
 }
 
 // Update is called once per frame
 void Update () {
     
     if(redCount == 1)
         chainBonus = 1;
     if(redCount == 2)
         chainBonus = 2;
     if(redCount == 3)
         chainBonus = 3;
     if(redCount == 4)
         chainBonus = 4;

 void OnTriggerEnter(Collider other) 
 {
     if (other.tag == "Red")
     {
         redCount = redCount + 1;
         blueCount = 0;
         Vector3 explosionPos = transform.position;
         exp = (GameObject)Instantiate(explosionPrefabRed, explosionPos, Quaternion.identity);
         Destroy(exp, explosionLife);
         other.transform.Translate(0,-4,0);//cheating way to fake death
         bonus = 100 * Timing.time;
         Scoring.score = Scoring.score + bonus * chainBonus;
         Destroy(gameObject);
     }
     
     if (other.tag == "Blue")
     {
         redCount = 0;
         blueCount = blueCount + 0;
         Vector3 explosionPos = transform.position;
         exp = (GameObject)Instantiate(explosionPrefabBlue, explosionPos, Quaternion.identity);
         Destroy(exp, explosionLife);
         bonus = 100 * Timing.time;
         Scoring.score = Scoring.score + bonus;
         Destroy(gameObject);
     }

 
  void  OnGUI (){
  GUI.Box(new Rect (Screen.width / 2 - 75,10,150,30), bonus.ToString("#") + "  total Score");
     GUI.Box(new Rect (Screen.width / 2 - 75,80,150,30), chainBonus + "  chain");
         GUI.Box(new Rect (Screen.width / 2 - 75,120,150,30), "red" + redCount);
     
 }
 }


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 syclamoth · Oct 30, 2011 at 03:49 AM

First up- in your Update, why don't you just use

 chainBonus = redCount;

instead of your confusing chain of if statements?

In any case, you're almost there- why don't you use this-

 if(redCount > blueCount)
 {
     chainBonus = redCount;
 } else {
     chainBonus = blueCount;
 }

You already reset the opposite counter when you break combo, so just use this to get the current bonus.

Is there a cap on the bonus multiplier? If so, use this-

 chainBonus = Mathf.Clamp(chainBonus, 0, maxMultiplier);
Comment
Add comment · Show 3 · 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 8r3nd4n · Nov 03, 2011 at 09:24 AM 0
Share

Thanks for that, works well and a lot neater. Also, the chainBonus, red and blueCounts need to be static otherwise they weren't picked up by the onTriggerEnter functions.

avatar image syclamoth · Nov 03, 2011 at 09:29 AM 0
Share

Why do they need to be static? You gave a reason there, sure, but it doesn't seem to be a very good one. In my experience there is always another way aside from using static variables, especially for things which are actually functional (not just class functions). Static variables force you into having exactly one instance of an object.

avatar image 8r3nd4n · Nov 03, 2011 at 10:03 AM 0
Share

For some reason, when they weren't static, they would not get passed into the OnTriggerEnter function properly (kept co$$anonymous$$g back with no value) or show their changes in the onGUI. While Im sure that there is a way to do it, I could only get it to work this way

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

PlayerPrefs is acting weird ?? 1 Answer

Adding coin score to multiplied value 2 Answers

Show score on gameover screen? 1 Answer

Count scripting problem 1 Answer

Show Current Score on GAMEOVER Scene 0 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