- Home /
Too subjective and argumentative
How to get multiple lives
I have a game where every time the main character passes a goal at a certain time, it earns five extra lives along with five hundred points. Also, when the character reaches 100 points, it earns a single extra life. But I do not know how to program the first part. In fact, every time I finish a scene after I get the 500 points, when I destroy an enemy or obtain more points, I earn yet another extra life. Here is my script so far. The source of the problem is there somewhere.
GameStatus.cs: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class GameStatus : MonoBehaviour
{
public static int score = 0;
public static int lives = 9;
public int target = 100;
public AudioSource myAudioSource;
public AudioClip bell;
public int bonusPoints = 500;
// Start is called before the first frame update
void Start()
{
myAudioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
}
void OnDestroy()
{
Debug.Log("GameStatus was destroyed.");
}
public void AddScore(int s)
{
score += s;
if (score >= target)
{
AddLives();
target += 100;
myAudioSource.PlayOneShot(bell);
}
}
public void AddLives()
{
lives++;
}
public void LoseLives()
{
lives--;
}
public void Bonus()
{
score += 500;
if (score >= bonusPoints)
{
lives += 5;
target += 500;
myAudioSource.PlayOneShot(bell);
}
}
}
PlayerController.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;
using System.IO;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D myBod;
public AudioSource myAudioSource;
public int moveOnDelay = 4;
public AudioClip crowd;
public AudioClip cent;
public AudioClip net;
public AudioClip bell;
public AudioClip splash;
public AudioClip bat;
public CountdownTimer ct;
public Vector3 respawnPoint;
private UnitManager gameUM;
private HealthManager hm;
public AudioClip takeSound;
public int damageTaken = 1;
public Transform firePoint;
public GameObject ninjaStar;
public GameStatus gs;
// Start is called before the first frame update
void Start()
{
myBod = GetComponent<Rigidbody2D>();
ct = FindObjectOfType<CountdownTimer>();
myAudioSource = GetComponent<AudioSource>();
gameUM = FindObjectOfType<UnitManager>();
hm = FindObjectOfType<HealthManager>();
respawnPoint = transform.position;
gs = FindObjectOfType<GameStatus>();
if (GlobalControl.Instance.IsSceneBeingLoaded)
{
PlayerState.Instance.localPlayerData = GlobalControl.Instance.LocalCopyOfData;
transform.position = new Vector3(
GlobalControl.Instance.LocalCopyOfData.PositionX,
GlobalControl.Instance.LocalCopyOfData.PositionY,
GlobalControl.Instance.LocalCopyOfData.PositionZ + 0.1f);
GlobalControl.Instance.IsSceneBeingLoaded = false;
}
if (Input.GetKeyDown(KeyCode.Return))
{
Instantiate(ninjaStar, firePoint.position, firePoint.rotation);
}
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
Vector2 origin = Vector2.zero;
if (Input.GetButtonDown("Jump"))
{
myBod.velocity = new Vector2(x * 5, 7f);
}
else
{
myBod.velocity = new Vector2(x * 5, myBod.velocity.y);
}
if (Input.GetKeyDown(KeyCode.Return))
{
Instantiate(ninjaStar, firePoint.position, firePoint.rotation);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "coin")
{
int coinScore = 10;
Destroy(other.gameObject);
gs.AddScore(coinScore);
myAudioSource.PlayOneShot(cent);
}
if (other.gameObject.tag == "obstacle" || other.gameObject.tag == "enemy")
{
HealthManager.HurtPlayer(damageTaken);
myAudioSource.PlayOneShot(takeSound);
}
if (other.gameObject.tag == "FallDetector")
{
gameUM.LifeLost();
myAudioSource.PlayOneShot(splash);
}
if (other.gameObject.tag == "bottle")
{
hm.FullHealth();
myAudioSource.PlayOneShot(crowd);
}
if (other.gameObject.tag == "goal" && ct.countingTime <= 10f)
{
ct.myAudioSource.Stop();
gs.Bonus();
StartCoroutine("MoveToNextUnit");
}
if (other.gameObject.tag == "goal" && ct.countingTime > 10f)
{
int bonusScore = (int)(ct.countingTime) * 2;
gs.AddScore(bonusScore);
myAudioSource.PlayOneShot(net);
StartCoroutine("MoveToNextUnit");
}
}
public IEnumerator MoveToNextUnit()
{
yield return new WaitForSeconds(moveOnDelay);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
Any assistance would be appreciated.
Sincerely, burchland2
There doesn't seem to be anything wrong with your script. What's the issue? Like what do you want to happen?
Answer by KloverGames · Nov 18, 2021 at 01:30 PM
Just say lives += 1;
Are you calling your function from a button? If you aren’t executing your function from Update() or Start() or from a button then nothing is going to happen at all…
I think your problem is that you’re creating functions but not executing them
I've tried calling my function through update, but then, when I destroy an enemy or obtain more points, I earn yet another extra life. Let me edit my original post.
Edit: I just edited the post. Please tell me what else I should do in order to fix this.
You need an if statement to tell the system when to execute your function. You aren’t supposed to just place it in Update()
Answer by Bassem102 · Nov 18, 2021 at 03:22 PM
Hello @burchland2 , You should Make some debug and see if this code here gets executed.
if (other.gameObject.tag == "goal" && ct.countingTime <= 10f)
{
ct.myAudioSource.Stop();
gs.Bonus();
StartCoroutine("MoveToNextUnit");
}
You probably have some wrong conditions or something.
It does execute, but after I earned the bonus points and lives, on the next stage, every time I earn more points, I gain yet another extra life. How should I fix this?
You said “on the next stage”, are you loading up a new scene? Or is it all one scene?
Follow this Question
Related Questions
Lives dont minus correctly 1 Answer
Player Health Not Working? 4 Answers
NullReferenceException error, GameObject.Find. 1 Answer
Script for getting in and out of a tank 2 Answers