Question by
st_asalak · Jun 13, 2019 at 06:01 PM ·
invokeinvokerepeatinglivessubtracting
How to subtract live when a game object destroy itself?
Hello, I'm making a simply whac-a-mole game and i have trouble about life system. I have basicly 12 spawn points and the program spawns randomly gameObjects on spawn points. When we click on those gameobjects destroys and gain 10 score points. If we don't click gameobject in 'destroyTime' then it destroys itself in destroyTime(2f). Everything works perfectly at this point. But i want to subtract lives when we miss the gameobject or don't click on in time it's actually works but only subtract 1 life point and it start to count debug logs only in 2.
Here is my script...
public class gameSystem : MonoBehaviour {
// Lives
public GUIText livesText;
private int lives;
private int livesPenalty;
// Timed Spawner
public GameObject spawnObject;
public bool stopSpawning = false;
public float spawnTime;
public float spawnDelay;
// Timed Destroy
private float destroyTime;
bool isDestroyed = false;
void Start () {
// Set the starter values of game. This void only read when game start from zero
score = 0;
// Update the starter GUI values of game.
// Timed Spawner
spawnDelay = Random.Range(4f, 10f);
spawnTime = Random.Range(4f, 10f);
InvokeRepeating("SpawnObject", spawnDelay, spawnDelay);
// Timed Destroyer
destroyTime = 2f;
Invoke("TimedDestroy", destroyTime);
// Subtracting Lives
livesPenalty = 1;
lives = 3;
livesText.text = "Lives: " + lives;
InvokeRepeating("SubtractingLives", destroyTime, spawnDelay);
}
void Update () {
if (Input.GetMouseButtonDown(0))
{
// Destroy game object when click on it
Vector2 mousePos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
RaycastHit2D hit = Physics2D.Raycast(mousePos, Vector2.zero, 100f);
if (hit.collider != null)
{
Destroy(hit.collider.gameObject);
// When we destroyed an object add 'scoreValue' value to our 'score'
if (hit.collider.gameObject != null)
{
score += scoreValue; // Addind score value for each objects we destroyed
scoreText.text = "Score: " + score; // Update our main score
//Debug.Log(score); // Print our current main score to console
CancelInvoke("TimedDestroy");
CancelInvoke("SubtractingLives");
}
}
}
}
public void TimedDestroy()
{
Destroy(GameObject.FindGameObjectWithTag("destroy"));
}
void SubtractingLives()
{
if (spawnObject == null)
{
isDestroyed = true;
if (lives == 3 && isDestroyed == true)
{
lives = lives - livesPenalty;
livesText.text = "Lives: " + lives;
Debug.Log("Lives " + lives);
isDestroyed = false;
}
if (lives == 2 && isDestroyed == true)
{
lives -= livesPenalty;
livesText.text = "Lives: " + lives;
Debug.Log("Lives: " + lives);
isDestroyed = false;
}
if (lives == 1 && isDestroyed == true)
{
lives -= livesPenalty;
livesText.text = "Lives: " + lives;
Debug.Log("Game Over: " + lives);
isDestroyed = false;
}
}
}
ekran-alıntısı.png
(46.1 kB)
Comment