I am improving on a class project and trying to implement a multiplier. There is a counter, and every time the counter hits 5, the multiplier should increase by one, with a max of 5. Then, the score gets updated. However, the multiplier is not taking effect. I also am not receiving any errors. Currently, this scoring script is the only one active.
if (other.tag == “Bullet”) {
other.GetComponent ().Play ();
asteroidHit++;
if (((asteroidHit / 5.0f) == (asteroidHit / 5)) && multiplier<5) {
multiplier += 1;
}
GameObject.Find (“Ship”).GetComponent().score += 100*multiplier;
Destroy (gameObject);
}
I take it that this is put on each asteroid.
So if it is hit by a “bullet” then you increase the asteroid by 1 (asteroidHit++)
Next you ask if multiplier is < 5 (remember, asteroidHit / 5.0f always equals asteroidHit / 5.0f).
if so, you increase the mutiplier by 1.
next, and this is real important, who cares what multiplier is or asteroidHit, you simply destroy the asteroid and add 100 * multiplier.
So questions…
What makes the counter go up?
If you destroy the game object, you do know you destroy this script?
Here is more what I think you mean.
public int health = 5;
public int multiplier = 0;
public AudioClip explosionSound;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Bullet") {
CreateExplosion(other.transform.position);
Destroy(other.gameObject);
health --;
multiplier++;
if(health <= 0){
CreateExplosion(transform.position);
var ship = GameObject.Find ("Ship");
ScoreKeeper scoreKeeper = null;
if(ship != null) ship.GetComponent<ScoreKeeper>();
if(scoreKeeper != null)scoreKeeper.score += 100 * multiplier;
Destroy(gameObject);
}
}
}
// create or copy an explosion game object
private CreateExplosion(Vector3 position){
var go = new GameObject();
go.transform.position = transform.position();
var player = go.AddComponent<AudioSource>();
player.PlayOneShot(explosionSound);
Destroy(go, 5);
}
this is the entire method minus Start() and Update() (which only address asteroid movement)
public int asteroidHit = 0;
public int multiplier = 1;
void OnTriggerEnter (Collider other){
if (other.tag == “Player”) {
PlayerPrefs.SetInt (“Score”, GameObject.Find (“Ship”).GetComponent().score);
PlayerPrefs.Save ();
Application.LoadLevel (“GameOver”);
}
if (other.tag == “Bullet”) {
other.GetComponent ().Play ();
asteroidHit++;
if (((asteroidHit / 5.0f) == (asteroidHit / 5)) && multiplier<5) {
multiplier += 1;
}
GameObject.Find (“Ship”).GetComponent().score += 100*multiplier;
Destroy (gameObject);
}
}
1: please use code tags (makes reading this stuff easier
2: your problem still remains the same. if you put this on an asteroid, multiplier will NEVER go above 2. This is because as soon as you add one to it, you destroy the script. Furthermore, asteroidHit will NEVER go above 1 because of the same reason.
Please be specific about what you are expecting to happen.
public int asteroidHit = 0;
public int multiplier = 1;
void OnTriggerEnter (Collider other){
if (other.tag == "Player") {
PlayerPrefs.SetInt ("Score", GameObject.Find ("Ship").GetComponent<ScoreKeeper>().score);
PlayerPrefs.Save ();
Application.LoadLevel ("GameOver");
}
if (other.tag == "Bullet") {
// a bullet has hit the asteroid....
// play a sound
other.GetComponent<AudioSource> ().Play ();
// add one to the asteroid hit
asteroidHit++;
// if the multiplier is less than 5, add one (make it 2)
if (multiplier<5) {
multiplier += 1;
}
// add 200 points to the scorekeeper
GameObject.Find ("Ship").GetComponent<ScoreKeeper>().score += 100*multiplier;
// DESTROY THE OBJECT SO THE ADDITIONS DONT MATTER ANYMORE!!!!!
Destroy (gameObject);
}
}