Here is the code:
GridSpace.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GridSpace : MonoBehaviour
{
public Button button;
public Text buttonText;
private GameController gameController;
public void Set()
{
buttonText.text = gameController.GetplayerSide();
button.interactable = false;
}
public void SetgameController(GameController controller)
{
gameController = controller;
}
}
GameController.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
public Text[] buttonList = new Text[9];
void SetgameControllerOnButtons()
{
for (int i = 0; i < buttonList.Length; i++)
buttonList[i].GetComponentInParent<GridSpace>().SetgameController(this);
}
void Init()
{
SetgameControllerOnButtons();
}
public string GetplayerSide()
{
return "1";
}
}
I get this error on line 14:
NullReferenceException: Object reference not set to an instance of an object
GridSpace.Set () (at Assets/Scripts/GridSpace.cs:14)
I can’t see what’s wrong… Telling me what to fix would be great! Also explaining me what’s the issue in my code would be much appreciated.
P.S: I just started to work in Unity.