Closed head-iie-vnr closed 4 months ago
The total coins are 4 on the board. If the collected coin count matches then set the WinText.
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class ScoreManager : MonoBehaviour
{
public static ScoreManager instance;
public TextMeshProUGUI scoreText;
public GameObject winText;
private int score = 0;
private static int MAX_COINS = 4;
void Awake()
{
Debug.Log("Called Score Manager");
if (instance == null)
{
instance = this;
Debug.Log("new instane is created");
winText.gameObject.SetActive(false);
}
else
{
Destroy(gameObject);
}
}
void Start()
{
UpdateScoreText();
}
public void AddScore(int amount)
{
score += amount;
UpdateScoreText();
if (score == MAX_COINS) {
Debug.Log("You Win");
winText.gameObject.SetActive(true);
}
}
void UpdateScoreText()
{
scoreText.text = "Score: " + score;
}
}
ScoreManager > Inpector > 'Score Manager' script > variables section Bind the variables to the UI components
Now you should see that WinText will be shown once you collect all the coins.
Edit the settings of the WinText properly
Attach the script as component to the GameoutArea object.
Set colors to the plane. Enable 'Collide' and Trigger enabled.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class GameoutArea : MonoBehaviour
{
public TextMeshProUGUI winText;
void OnTriggerEnter(Collider other)
{
Debug.Log("Ball fell off the board.");
if (other.gameObject.CompareTag("Player"))
{
winText.text = "Oops.. That's Sad!!";
winText.gameObject.SetActive(true);
}
}
}
When the player falls off the board, it shows like below.
I have renamed the variable 'winText' as 'GameEndText' so that it can be used for both win & game over criteria.
Download audios from freeaudio.org
In the ScoreManager.cs
Add instance public variable.
public AudioSource countSound;
Update the AddScore method to play the sound
public void AddScore(int amount)
{
score += amount;
countSound.Play();
UpdateScoreText();
if (score == MAX_COINS) {
Debug.Log("You Win");
winText.gameObject.SetActive(true);
Time.timeScale = 0;
}
}
Add 'AudioSource' component to the ScoreManager UI component.
Link the 'Coin Sound' to ScoreManager.
Question : If two sounds are added in Same ScoreManager UI object, then both will get played. How to stop it???
The best strategy is to, create audio UI components which have one unique sound with them.
Add 'AudioSource' component to the UI elements. Then link them to the ScoreManager
This will make this unambigious & easy to maintain.
Note: remove Play on Awake attribute to those Audio Source components added
Decide on the Position & rotation values for each zone.
Zone1: Position (0, 40, -40), Rotation (45, 0, 0)
Position (-40, 40, 0), Rotation (45, 90, 0)
Position (0, 40, 40) Rotation (45, 180, 0)
Zone4: Position (40, 40, 0) Rotation (45, 270, 0)
For sounds signup at
ScoreManager.cs
public void AddScore(int amount)
{
score += amount;
if (amount == 1) {
coinSound.Play();
} else {
goldenBallCoinSound.Play();
}
coinsCollected++;
UpdateScoreText();
if (coinsCollected == MAX_COINS) {
Debug.Log("You Win");
winText.gameObject.SetActive(true);
Time.timeScale = 0;
}
}
Added Golden ball If the coin is collected using the golden ball then there will be bonus score.
Game will be over when all coins are collected, not by score achieved.
Scope