Double Points Powerup
I am trying to make a double points powerup and am really close to figuring it out! I created a multiplier in my scoremanager and set it equal to one. When the player collides with the X2 object the multiplier is incremented by 1 (multiplier++). My code works briefly, but then the score starts getting ridiculously high. I decremented the multiplier (multiplier--) after the powerup duration is finished, but that does not seem to work. I would appreciate any help.
Here my code:
Code1:
using UnityEngine;
using System.Collections;
public class Multiplier : MonoBehaviour {
public GameObject particle;
public AudioClip powerUpSound;
public float time;
bool Active=false;
void Update(){
if (Active==false) {
StartCoroutine (DestroyGameObject ());
}
}
IEnumerator ScoreMultiplier(){
Active = true;
ScoreManager .multiplier ++;
yield return new WaitForSeconds (time);
ScoreManager.multiplier--;
Destroy (gameObject);
}
IEnumerator DestroyGameObject(){
yield return new WaitForSeconds (3);
gameObject.GetComponent <SpriteRenderer > ().enabled = false;
yield return new WaitForSeconds (17);
Destroy (gameObject);
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Player") {
Active = true;
gameObject.GetComponent <SpriteRenderer > ().enabled = false;
StartCoroutine (ScoreMultiplier ());
}
}
}
Code 2 (Attached to player):
using UnityEngine;
using System.Collections;
using UnityEngine .Audio ;
using UnityEngine .SceneManagement ;
public class Player : MonoBehaviour {
private int Base_Reward1=100;
private int Base_Reward2=200;
private int Base_Reward3=300;
public void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Running Player1") {
ScoreManager.scoreCount += Base_Reward1*=ScoreManager .multiplier;
Destroy (other.gameObject);
}
}
}
Answer by Alec-Slayden · May 31, 2016 at 08:10 PM
It looks like you might be destroying the score object before it has a chance to decrease the multiplier again.
if I understand correctly, when the player collides with the bonus trigger, the bonus hides its sprite, increases the multiplier, then begins to wait for a little while before decreasing the multiplier and self-destructing.
however, in the player script, you destroy the bonus game object immediately, so that coroutine will stop before it has a chance to decrease the multiplier.
Consider using OnDestroy() to clear the bonus.
Thanks for getting back to me.
I should have been a little more clear in my description. The "Running Player1" object is not the powerup. When the player collides with the powerup the points earned are increased. Even when the powerup is not active the player will receive points when colliding with the "Running Player1." When I destroy the gameObject in the Player script it is not the powerup that I am destroying, but the "Running Player1." I don't think destroying this gameObject is the issue, but I could be wrong.
I just noticed something in my Player script (line 16) - Score$$anonymous$$anager.scoreCount += Base_Reward1*=Score$$anonymous$$anager .multiplier;
I should have it like this: Score$$anonymous$$anager.scoreCount += (Base_Reward1*Score$$anonymous$$anager .multiplier);
In the original code I did *= which would help clarify why the score was reaching an absurd amount. Won't be able to test until tonight. Hopefully this small error will fix the issue.
Your answer
