using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class EndScene : MonoBehaviour {
public Text retryButton;
public Text exitButton;
public Text roundsLasted;
public Text moneyEarned;
public Button Stats;
GameController scene;
public void Retry()
{
SceneManager.LoadScene(0);
scene.StartGame();
}
public void Exit()
{
Debug.Log("Game has quit");
Application.Quit();
}
private void Awake()
{
moneyEarned.text = "Total Money Earned: " + scene.totalMoneyEarned.ToString();
roundsLasted.text = "Rounds Lasted: " + scene.userRoundsLasted.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour {
int dealersFirstCard = -1;
public int user_Bet = 0;
public int user_Overall = 1000;
public int userRoundsLasted = 0;
public int totalMoneyEarned = 0;
public CardStack player;
public CardStack dealer;
public CardStack deck;
public Button hitButton;
public Button stickButton;
public Button doubleDownButton;
public Button playAgainButton;
public Button Bet5;
public Button Bet10;
public Button Bet25;
public Button Bet100;
public Button Bet1000;
public bool ddCheck = false;
public bool txtCheck = true;
public Text winnerText;
public Text betAmount;
public Text overallAmount;
public Text userValue;
public Text dealerValue;
public Text amountError;
#region Public Methods
public void Add5()
{
user_Bet = user_Bet + 5;
betAmount.text = "Bet: " + user_Bet.ToString();
}
public void Add10()
{
user_Bet = user_Bet + 10;
betAmount.text = "Bet: " + user_Bet.ToString();
}
public void Add25()
{
user_Bet = user_Bet + 25;
betAmount.text = "Bet: " + user_Bet.ToString();
}
public void Add100()
{
user_Bet = user_Bet + 100;
betAmount.text = "Bet: " + user_Bet.ToString();
}
public void Add1000()
{
user_Bet = user_Bet + 1000;
betAmount.text = "Bet: " + user_Bet.ToString();
}
public void Hit()
{
if (user_Overall >= user_Bet)
{
player.Push(deck.Pop());
if (player.HandValue() > 21)
{
hitButton.interactable = false;
stickButton.interactable = false;
doubleDownButton.interactable = false;
StartCoroutine(DealersTurn());
}
}
else
{
amountError.text = "You do not have enough money";
}
}
public void Stick()
{
if (user_Overall >= user_Bet)
{
hitButton.interactable = false;
stickButton.interactable = false;
doubleDownButton.interactable = false;
StartCoroutine(DealersTurn());
}
else
{
amountError.text = "You do not have enough money";
}
}
public void IsBust()
{
if (user_Overall == 0)
{
SceneManager.LoadScene(1);
}
}
public void DoubleDown()
{
if (user_Overall >= user_Bet *2)
{
player.Push(deck.Pop());
user_Bet = user_Bet + user_Bet;
hitButton.interactable = false;
stickButton.interactable = false;
doubleDownButton.interactable = false;
betAmount.text = "Bet: " + user_Bet.ToString();
ddCheck = true;
StartCoroutine(DealersTurn());
}
else
{
amountError.text = "You do not have enough money";
}
}
public void Clear(){
user_Bet = 0;
betAmount.text = "Bet: " + user_Bet.ToString();
}
public void PlayAgain()
{
playAgainButton.interactable = false;
player.GetComponent<CardStackView>().Clear();
dealer.GetComponent<CardStackView>().Clear();
deck.GetComponent<CardStackView>().Clear();
deck.CreateDeck();
hitButton.interactable = true;
stickButton.interactable = true;
dealersFirstCard = -1;
winnerText.text = "";
dealerValue.text = "";
StartGame();
}
#endregion
#region Unity messages
public void Start()
{
StartGame();
}
#endregion
public void StartGame()
{
for (int i = 0; i < 2; i++)
{
player.Push(deck.Pop());
HitDealer();
}
}
void HitDealer()
{
int card = deck.Pop();
if (dealersFirstCard < 0){
dealersFirstCard = card;
}
dealer.Push(card);
if (dealer.CardCount >= 2)
{
CardStackView view = dealer.GetComponent<CardStackView>();
view.Toggle(card, true);
}
}
IEnumerator DealersTurn()
{
hitButton.interactable = false;
stickButton.interactable = false;
CardStackView view = dealer.GetComponent<CardStackView>();
view.Toggle(dealersFirstCard, true);
view.ShowCards();
yield return new WaitForSeconds(1f);
while (dealer.HandValue() < 17)
{
HitDealer();
yield return new WaitForSeconds(1f);
}
dealerValue.text = dealer.HandValue().ToString();
if (player.HandValue() > 21 || (dealer.HandValue() > player.HandValue() && dealer.HandValue() <= 21))
{
winnerText.text = "Dealer Wins";
user_Overall = user_Overall - user_Bet;
overallAmount.text = "Overall: " + user_Overall.ToString();
}
else if (dealer.HandValue() >21 || (player.HandValue() <= 21 && player.HandValue() > dealer.HandValue()))
{
winnerText.text = "Player Win";
if (ddCheck == true)
{
user_Overall = user_Overall + user_Bet + user_Bet;
overallAmount.text = "Overall: " + user_Overall.ToString();
totalMoneyEarned = totalMoneyEarned + user_Bet;
}
else
{
user_Overall = user_Overall + user_Bet;
overallAmount.text = "Overall: " + user_Overall.ToString();
}
}
else if (dealer.HandValue() == player.HandValue())
{
winnerText.text = "Push";
}
else
{
winnerText.text = "The House Wins";
}
yield return new WaitForSeconds(1f);
playAgainButton.interactable = true;
userRoundsLasted = userRoundsLasted + 1;
}
private void Update()
{
userValue.text = player.HandValue().ToString();
IsBust();
}
}
Hi,
I am developing a blackjack game and once the user has gone bust I want it to show a screen with the total amount of money they earned and how many rounds they lasted and I am keeping track of these stats in the “Game Controller” script, and then I want to output these values to the screen. However, I keep getting the error message “A null value was found where an object instance was required.” for the above code. The code that is highlighted when I click on the error message is "moneyEarned.text = “Total Money Earned: " + scene.totalMoneyEarned.ToString();” and I think it has something to do with the fact that I am not calling the “Game Controller” script correctly as when I remove the bit of code “+ scene.totalMoneyEarned.ToString();” The first bit of the text is outputted correctly and therefore I assume the reason is the same for why the “userRoundsLasted” isn’t being displayed as well.
Any help is much appreciated.
Thanks