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();
}
// 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();
ct = FindObjectOfType();
myAudioSource = GetComponent();
gameUM = FindObjectOfType();
hm = FindObjectOfType();
respawnPoint = transform.position;
gs = FindObjectOfType();
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
↧