In my project, I have a MonoBehaviour class, PlayerController, that contains an array of objects of another MonoBehaviour class, WaypointController. This array is populated by dragging objects into the Inspector. The problem is that the objects in the array start out fine when testing the game, but become null, and I cannot figure out why or exactly where. I am not destroying the objects (knowingly) or getting to the point where I load other scenes. How can I fix this?
Each WaypointController is attached to a Waypoint game object that contains an initially invisible CanvasGroup. When the Player (to which PlayerController is attached) arrives at a Waypoint, the canvas group becomes visible, and once the Player clicks on a button, the canvas group becomes invisible again. PlayerController.ContinueTour() is the function called when the button is clicked. This works for the first Waypoint in the array, but, by the the time the second Waypoint is reached, all the Waypoints are null.
This is the relevant part of PlayerController class:
public class PlayerController : MonoBehaviour {
// This is the array that is populated from the Inspector
// by dragging and dropping objects.
public WaypointController[] waypoints;
int currentWaypoint = 0;
// Checks if any waypoints are null.
void CheckWaypoints() {
Debug.Log("Checking waypoints");
if (waypoints == null) {
Debug.Log ("waypoints array is null");
}
for (int i = 0; i < waypoints.Length; i++) {
if (waypoints [i] == null) {
Debug.Log ("Waypoint " + i + " is null.");
}
}
}
// Called whenever a button in a WaypointController's canvas is clicked.
public void ContinueTour() {
CheckWaypoints ();
if (AtWaypoint() && waypoints [currentWaypoint] != null) {
// Hide the description.
waypoints [currentWaypoint].HideDescription ();
// If this is the last waypoint in the circuit,
// load the next scene.
if (currentWaypoint == waypoints.Length - 1) {
LoadNextScene ();
}
// If there are more waypoints, continue to the next waypoint.
else {
currentWaypoint++;
isMoving = true;
}
} else {
Debug.Log ("ERROR: Something's wrong... Either we have not arrived at the waypoint, or the waypoint is null");
}
}
And this is the WaypointController class:
public class WaypointController : MonoBehaviour {
Canvas waypointDescriptionCanvas;
CanvasGroup canvasGroup;
void Awake() {
waypointDescriptionCanvas = GetComponentInChildren<Canvas> ();
if (waypointDescriptionCanvas == null) {
ConsoleLogger.Log ("Could not get canvas.");
} else {
ConsoleLogger.Log ("Disabling description");
canvasGroup = waypointDescriptionCanvas.GetComponent<CanvasGroup>();
HideDescription();
}
}
// Make the description canvas visible.
public void ShowDescription() {
ConsoleLogger.Log ("ShowDescription");
Vector3 direction = waypointDescriptionCanvas.transform.position - Camera.main.transform.position;
waypointDescriptionCanvas.transform.forward = direction;
canvasGroup.alpha = 1;
canvasGroup.interactable = true;
}
public void HideDescription() {
ConsoleLogger.Log ("HideDescription");
if (canvasGroup != null) {
canvasGroup.alpha = 0;
canvasGroup.interactable = false;
} else {
Debug.Log ("canvasGroup is null");
}
}
}