such as the question, in the actual operation, I found that the direct allocation of space in the GameManager class and the allocation of space in the Awake function, and finally the result of Debug in the UILayoout class is different. I don"t know why.
finally, the output of the Awake allocation result is normal.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour {
private static GameManager _instance;
public static GameManager Instance
{
get
{
if (_instance == null)
{
GameObject gm = new GameObject("GameManager");
gm.AddComponent<GameManager>();
}
return _instance;
}
}
//public PlayerSetting playerSetting = new PlayerSetting();
//
public PlayerSetting playerSetting;
void Awake()
{
_instance = this;
DontDestroyOnLoad(this);
playerSetting = new PlayerSetting();
//Awake
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
[Serializable]
public class PlayerSetting
{
public KeyCode[] defaultButton = new KeyCode[12];
public KeyCode[] currentButton = new KeyCode[12];
public int volume;
public PlayerSetting()
{
defaultButton[0] = KeyCode.W;
defaultButton[1] = KeyCode.A;
defaultButton[2] = KeyCode.S;
defaultButton[3] = KeyCode.D;
defaultButton[4] = KeyCode.Alpha1;
defaultButton[5] = KeyCode.Alpha2;
defaultButton[6] = KeyCode.Alpha3;
defaultButton[7] = KeyCode.Alpha4;
defaultButton[8] = KeyCode.Space;
defaultButton[9] = KeyCode.E;
defaultButton[10] = KeyCode.F;
defaultButton[11] = KeyCode.Z;
for (int i = 0; i < defaultButton.Length; iPP)
{
currentButton[i] = defaultButton[i];
}
volume = 50;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.IO;
public class UILayout : MonoBehaviour
{
// Use this for initialization
private void Awake()
{
}
private void Start()
{
Debug.Log(GameManager.Instance.playerSetting.currentButton.Length);
//
//11
//Awake12
}