I’m not sure if I am missing something super simple, but I am getting “NullReferenceException: Object reference not set to an instance of an object,” which points to a line of code (commented below) which is trying to access the script of a game object in the scene.
I have three game objects that are sharing the same script to perform animations only when they are asked to do so, not simultaneously. Here is my code in the GameController, which is trying to access a script of the game object:
public class GameController : MonoBehaviour {
GameObject hp1, hp2, hp3;
HockeyPlayerController hp1Control, hp2Control, hp3Control;
void Start ()
{
// Find the hockey players
hp1 = GameObject.FindGameObjectWithTag("HockeyPlayer1");
hp2 = GameObject.FindGameObjectWithTag("HockeyPlayer2");
hp3 = GameObject.FindGameObjectWithTag("HockeyPlayer3");
// Get hockey players' controller scripts
hp1Control = hp1.GetComponent<HockeyPlayerController>();
hp2Control = hp2.GetComponent<HockeyPlayerController>();
hp3Control = hp3.GetComponent<HockeyPlayerController>();
}
void Update ()
{
hp2Control.hpShoot(); // NullReferenceException
}
For now I am just trying to test it, but the following error is keeping me from doing so.
EDIT: Solved (please delete this post)
Solution: hp1Control = hp1.GetComponent(“HockeyPlayerController”) as HockeyPlayerController;
…