Reputation: 1
I'm making a bullet hell game where the longer you survive, the more score you get. The score timer starts at 1 and goes up when playing. However, I want it to stop when the player dies, but I can't get it quite right.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScorePerSecond : MonoBehaviour
{
public Text scoreText;
public float scoreAmount;
public float pointIncreasedPerSecond;
private string ENEMY_TAG = "Enemy";
private string FOLLOWENEMY_TAG = "Followenemy";
private string PLAYER_TAG = "Player";
private bool isFinished = false;
// Start is called before the first frame update
void Start()
{
isFinished = true;
scoreAmount = 0f;
pointIncreasedPerSecond = 5f;
}
// Update is called once per frame
void Update()
{
if (isFinished == true)
{ scoreText.text = "Survived time " + (int)scoreAmount;
scoreAmount += pointIncreasedPerSecond * Time.deltaTime;
isFinished = true;
}
else
{
}
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag(PLAYER_TAG))
Destroy(gameObject);
{
isFinished = false;
}
}
//if (collision.gameObject.CompareTag(FOLLOWENEMY_TAG))
// Destroy(gameObject);
}
Upvotes: 0
Views: 212
Reputation: 90639
So there is a lot of strange things going on in your code!
First of all you probably do not want to set
isFinished = true;
right from the beginning. Otherwise you want to change the naming ...
Then note that in
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag(PLAYER_TAG))
Destroy(gameObject);
{
isFinished = false;
}
}
What you actually do is
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag(PLAYER_TAG))
{
Destroy(gameObject);
}
isFinished = false;
}
so no matter what you collide with you change the state of isFinished
. You probably rather wanted to make the brackets wrap both lines.
And then checking and setting
if(isFinished == true)
{
...
isFinished = true;
}
is quite redundant.
And last but not least: You don't need the isFinished
at all!
Since you do
Destroy(gameObject);
this Update
method anyway will not be executed anymore. So unless there are not multiple instances of this script writing into the same Text
it should stop as soon as the object was destroyed anyway.
public class ScorePerSecond : MonoBehaviour
{
private const string ENEMY_TAG = "Enemy";
private const string FOLLOWENEMY_TAG = "Followenemy";
private const string PLAYER_TAG = "Player";
public Text scoreText;
public float scoreAmount;
// Instead of overwriting this hardcoded allow to set it via the Inspector
public float pointIncreasedPerSecond = 5f;
private void Start()
{
scoreAmount = 0f;
}
private void Update()
{
// you probably want to increase the value first and then update the display
// otherwise you always only see the vlaue of the previous frame
scoreAmount += pointIncreasedPerSecond * Time.deltaTime;
scoreText.text = "Survived time " + (int)scoreAmount;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag(PLAYER_TAG))
{
Destroy(gameObject);
}
}
}
What stays quite unclear to me: Here no player is dying. You rather check if you collide with a player and kill this object itself.
Did you maybe mean to rather do
Destroy(other.gameObject);
-> In that case, yes you would need the isFinished
again.
public class ScorePerSecond : MonoBehaviour
{
private const string ENEMY_TAG = "Enemy";
private const string FOLLOWENEMY_TAG = "Followenemy";
private const string PLAYER_TAG = "Player";
public Text scoreText;
public float scoreAmount;
// Instead of overwriting this hardcoded allow to set it via the Inspector
public float pointIncreasedPerSecond = 5f;
private bool isFinished;
private void Start()
{
scoreAmount = 0f;
}
private void Update()
{
if(isFinished) return;
// you probably want to increase the value first and then update the display
// otherwise you always only see the vlaue of the previous frame
scoreAmount += pointIncreasedPerSecond * Time.deltaTime;
scoreText.text = "Survived time " + (int)scoreAmount;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag(PLAYER_TAG))
{
Destroy(other.gameObject);
isFinished = true;
}
}
}
Upvotes: 1