I have done a bit of research attempting to go around the error message and do research instead but I have only confused myself further. I do not understand what a null reference exactly is or how to avoid it in a tutorial I have been doing.
using UnityEngine;
using UnityEngine.UI;
public class GameControl : MonoBehaviour {
private static GameObject whoWinsTextShadow, player1MoveText, player2MoveText;
private static GameObject player1, player2;
public static int diceSideThrown = 0;
public static int player1StartWaypoint = 0;
public static int player2StartWaypoint = 0;
public static bool gameOver = false;
//use this for initialization
void Start () {
whoWinsTextShadow = GameObject.Find("WhoWinsText");
player1MoveText = GameObject.Find("Player1MoveText");
player2MoveText = GameObject.Find("Player2MoveText");
player1 = GameObject.Find("Player1");
player2 = GameObject.Find("Player2");
player1.GetComponent<FollowThePath>().moveAllowed = false;
player2.GetComponent<FollowThePath>().moveAllowed = false;
whoWinsTextShadow.gameObject.SetActive(false);
player1MoveText.gameObject.SetActive(true);
player2MoveText.gameObject.SetActive(false);
}
// Update is called once per frame
void Update()
{
if (player1.GetComponent<FollowThePath>().waypointIndex >
player1StartWaypoint + diceSideThrown)
{
player1.GetComponent<FollowThePath>().moveAllowed = false;
player1MoveText.gameObject.SetActive(false);
player2MoveText.gameObject.SetActive(true);
player1StartWaypoint = player1.GetComponent<FollowThePath>().waypointIndex - 1;
}
if (player2.GetComponent<FollowThePath>().waypointIndex >
player2StartWaypoint + diceSideThrown)
{
player2.GetComponent<FollowThePath>().moveAllowed = false;
player2MoveText.gameObject.SetActive(false);
player1MoveText.gameObject.SetActive(true);
player2StartWaypoint = player1.GetComponent<FollowThePath>().waypointIndex - 1;
}
if (player1.GetComponent<FollowThePath>().waypointIndex ==
player1.GetComponent<FollowThePath>().waypoints.Length)
{
whoWinsTextShadow.gameObject.SetActive(true);
whoWinsTextShadow.GetComponent<Text>().text = "Player 1 Wins";
gameOver = true;
}
if (player2.GetComponent<FollowThePath>().waypointIndex ==
player2.GetComponent<FollowThePath>().waypoints.Length)
{
whoWinsTextShadow.gameObject.SetActive(true);
whoWinsTextShadow.GetComponent<Text>().text = "Player 2 Wins";
gameOver = true;
}
}
public static void MovePlayer(int playerToMove)
{
switch (playerToMove) {
case 1:
player1.GetComponent<FollowThePath>().moveAllowed = true;
break;
case 2:
player2.GetComponent<FollowThePath>().moveAllowed = true;
break;
}
}
}
using System.Collections;
using UnityEngine;
public class Dice : MonoBehaviour
{
private Sprite[] diceSides;
private SpriteRenderer rend;
private int whosTurn = 1;
private bool coroutineAllowed = true;
private void Start (){
rend = GetComponent<SpriteRenderer>();
diceSides = Resources.LoadAll<Sprite>("DiceSides/");
rend.sprite = diceSides[5];
}
private void OnMouseDown()
{
if (!GameControl.gameOver && coroutineAllowed)
StartCoroutine("RollTheDice");
}
private IEnumerator RollTheDice()
{
coroutineAllowed = false;
int randomDiceSide = 0;
for (int i = 0; i <= 20; i++)
{
randomDiceSide = Random.Range(0, 6);
rend.sprite = diceSides[randomDiceSide];
yield return new WaitForSecond(0.05f);
}
GameControl.diceSideThrown = randomDiceSide + 1;
if (whosTurn == 1)
{
GameControl.MovePlayer(1);
} else if (whosTurn == -1)
{
GameControl.MovePlayer(2);
}
whosTurn *= -1;
coroutineAllowed = true;
}
}
using UnityEngine;
public class FollowThePath : MonoBehaviour
{
private static GameObject waypoint;
public Transform[] waypoints;
[SerializeField]
private float moveSpeed = 1f;
[HideInInspector]
public int waypointIndex = 0;
public bool moveAllowed= false;
private void Start () {
transform.position = waypoints[waypointIndex].transform.position;
}
private void Update () {
if (moveAllowed)
Move();
}
private void Move()
{
if (waypointIndex <= waypoints.Length - 1)
{
transform.position = Vector2.MoveTowards(transform.position,
waypoints[waypointIndex].transform.position,
moveSpeed * Time.deltaTime);
if (transform.position == waypoints[waypointIndex].transform.position)
{
waypointIndex +=1;
}
}
}
}