Basically, I am making a system where the user can pick the amount of players in the game, then the controls they want to use (they do this by either clicking WASD,Arrow Keys, Left analogue stick, or Right analogue stick), and then their colour. However, the code runs fine when I’ve selected the object the script is attached to, but whenever I select another object, anything in my player control array isn’t loaded.
For this I have created a class here:
public class Players
{
public int playerNum;
public int playerControl;
public Color colour;
public string name;
}
And use this as the array:[SerializeField] public Players[] playerControlTemp;
This allows me to store all the details of the characters when I transfer to the next scene.
I’ve added this to debug:
foreach (Players player in playerControlTemp)
{
Debug.Log(player.playerControl);
if (playerControlTemp[0] != null && player.playerControl == 0)
And whenever I select the object, I get the player controls, but when I’m selecting something else, I get “Object reference not set to an instance of an object”. If I select the object to load the array, then click off, it stills works. So clearly the issue is that unity isn’t loading the array until I have selected the object.
I am Initiating the array using this code from a separate script.
public void SelectNumOfCharacters()
{
settingManager.GetComponent<PlayerSettings>().playerControlTemp = new PlayerSettings.Players[numOfCharacters];
This is unbelievably annoying as there is no documentation on this, please help.
EDIT: The program doesn’t work on an executable, as nothing can be executed.
I have provided screenshots of my design.
Thanks