- Home /
Multiplying score on a gameobject destroy; only while a button is held
In my breakout clone, I want the player to earn 10 points for every block destroyed. However, if they break a block while the X key is held down, the points the blocks typically award is multiplied to 100. I've tried just about everything I know, and I can't get this to work as intended. This is my block/brick script. I also have a separate script for the scoring text UI itself but I don't know if it's of any use.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Brick : MonoBehaviour {
public AudioClip crack;
public AudioClip explode;
private int timesHit;
public static int breakableCount = 0;
public Sprite[] hitSprites;
private LevelManager levelManager;
public float delay = 0F;
private bool isBreakable;
// Use this for initialization
void Start () {
isBreakable = (this.tag == "Breakable");
timesHit = 0;
levelManager = GameObject.FindObjectOfType<LevelManager>();
if (isBreakable) {
breakableCount++;
}
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D (Collision2D col) {
AudioSource.PlayClipAtPoint (crack, transform.position);
if (isBreakable) {
HandleHits ();
}
}
void HandleHits () {
timesHit++;
int maxHits = hitSprites.Length + 1;
if (timesHit >= maxHits) {
breakableCount--;
levelManager.BrickDestroyed ();
AudioSource.PlayClipAtPoint (explode, transform.position);
Destroy (gameObject);
if (Input.GetKeyDown (KeyCode.Z)) {
Score.scoreValue += 10 * 10000;
} Score.scoreValue += 10;
} else LoadSprite();
}
void LoadSprite () {
int spriteIndex = timesHit - 1;
if (hitSprites [spriteIndex]) {
this.GetComponent<SpriteRenderer> ().sprite = hitSprites [spriteIndex];
}
}
//To Do: remove this when blocks are destructible
void SimulateWin () {
levelManager.LoadNextLevel();
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Best way to move a 2D Character 2 Answers