Here is the code could anyone tell me where i am going wrong please? I need the buttons to access my methods which will compare the two cards on one stat, giving the winning player a point but when i press these buttons i get the error 'NullReferenceException: Object reference not set to an instance of an object'
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameLogic : MonoBehaviour
{
public int player1Score = 5;
public int player2Score = 5;
public bool gameEnded = false;
public bool GameOver = false;
public Card[] player1Hand;
public Card[] player2Hand;
//public GameObject GameOverPannel;
public Text uitext;
public class Card
{
public string cardName;
public int year;
public int fame;
public int breathtaking;
public int everyday;
public int rating;
public int allCards;
public Card(string cardName, int year, int fame, int breathtaking, int everyday, int rating)
{
this.cardName = cardName;
this.year = year;
this.fame = fame;
this.breathtaking = breathtaking;
this.everyday = everyday;
this.rating = rating;
}
}
// Start is called before the first frame update
void Start()
{
Card AstonMartinDBSCard = new Card("AstonMartinDBS",1964, 99, 48, 11, 84);
Card BatmoblieCard = new Card("Batmoblie",1989, 100, 48, 3, 100);
Card BumblebeeCard = new Card("Bumblebee",2007, 87, 34, 29, 63);
Card DelorianCard = new Card("Delorian",1985, 98, 45, 14, 90);
Card GeneralLeeCard = new Card("GeneralLee",1979, 79, 28, 20, 71);
Card ECTO1Card = new Card("ECTO1",1984, 96, 46, 8, 68);
Card HerbieCard = new Card("Herbie",1968, 83, 22, 30, 64);
Card KITTCard = new Card("KITT",1982, 84, 28, 18, 83);
Card FootmobileCard = new Card("Footmobile",1960, 93, 41, 2, 43);
Card MysteryMachineCard = new Card("MysteryMachine",1969, 95, 31, 6, 52);
Card[] allCards = new Card[] { AstonMartinDBSCard, BatmoblieCard, BumblebeeCard, DelorianCard, GeneralLeeCard, ECTO1Card, HerbieCard, KITTCard, FootmobileCard, MysteryMachineCard };
// Shuffle the cards randomly
System.Random rng = new System.Random();
int n = allCards.Length;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
Card temp = allCards[k];
allCards[k] = allCards[n];
allCards[n] = temp;
}
player1Hand = new Card[5]{
allCards[0], allCards[1], allCards[2], allCards[3], allCards[4]
};
player2Hand = new Card[5]{
allCards[5], allCards[6], allCards[7], allCards[8], allCards[9]
};
/* foreach (Card card in player1Hand) {
uitext.text += "player 1's cards are " + card.cardName + "\n";
}*/
foreach (Card card in player1Hand) {
Debug.Log("player 1s cards are " + card.cardName);
}
foreach (Card card in player2Hand) {
Debug.Log("player 2s cards are " + card.cardName);
}
Debug.Log("player1Hand[1]: " + player1Hand[1]);
Debug.Log("player2Hand[1]: " + player2Hand[1]);
}
public void Year()
{
if (player1Hand[1].year > player2Hand[1].year)
{
Debug.Log("Player 1 wins the round!");
player1Score++;
player2Score--;
}
else if (player2Hand[1].year > player1Hand[1].year)
{
Debug.Log("Player 2 wins the round!");
player2Score++;
player1Score--;
}
else
{
Debug.Log("It's a tie!");
}
CheckWinner();
}
public void Fame()
{
if (player1Hand[2].fame > player2Hand[2].fame)
{
Debug.Log("Player 1 wins the round!");
player1Score++;
player2Score--;
}
else if (player2Hand[2].fame > player1Hand[2].fame)
{
Debug.Log("Player 2 wins the round!");
player2Score++;
player1Score--;
}
else
{
Debug.Log("It's a tie!");
}
CheckWinner();
}
public void Breathtaking()
{
if (player1Hand[3].breathtaking > player2Hand[3].breathtaking)
{
Debug.Log("Player 1 wins the round!");
player1Score++;
player2Score--;
}
else if (player2Hand[3].breathtaking > player1Hand[3].breathtaking)
{
Debug.Log("Player 2 wins the round!");
player2Score++;
player1Score--;
}
else
{
Debug.Log("It's a tie!");
}
CheckWinner();
}
public void Everyday()
{
if (player1Hand[4].everyday > player2Hand[4].everyday)
{
Debug.Log("Player 1 wins the round!");
player1Score++;
player2Score--;
}
else if (player2Hand[4].everyday > player1Hand[4].everyday)
{
Debug.Log("Player 2 wins the round!");
player2Score++;
player1Score--;
}
else
{
Debug.Log("It's a tie!");
}
CheckWinner();
}
public void Rating()
{
if (player1Hand[5].rating > player2Hand[5].rating)
{
Debug.Log("Player 1 wins the round!");
player1Score++;
player2Score--;
}
else if (player2Hand[5].rating > player1Hand[5].rating)
{
Debug.Log("Player 2 wins the round!");
player2Score++;
player1Score--;
}
else
{
Debug.Log("It's a tie!");
}
CheckWinner();
}
// to work out winners first to get 10 cards, do it by starting on, 5 +1 for win -1 for loss
public void CheckWinner()
{
if (player1Score == 10)
{
Debug.Log("Player 1 wins the game!");
gameEnded = true;
}
else if (player2Score == 10)
{
Debug.Log("Player 2 wins the game!");
gameEnded = true;
}
if (gameEnded == true)
{
//GameOver = true;
SceneManager.LoadScene("EndGameScreen");
}
}
}
I have tried testing different tings but nothing has worked