Search Unity

Script only work when I inspected it at least once

Discussion in 'Scripting' started by layaha, Jan 18, 2020.

  1. layaha

    layaha

    Joined:
    Jan 1, 2018
    Posts:
    7
    Hi everybody, thanks for reading.

    My problem:

    An object is getting a class array from a manager.

    It works perfectly when I inspected the manager gameobject at least once during the play mode.

    It doesn't work (null reference) when Im Not inspecting the manager gameobject.

    May I know what the problem is?


    Script:

    Object A:

    Code (CSharp):
    1.  
    2. void Start()
    3. {
    4. SetUpStat()
    5. }
    6.  
    7. void SetUpStat()
    8. {
    9. Manager.Instance.cornStatArray[_inventoryPlaceNum].inventoryPlaceNum = _inventoryPlaceNum;
    10. }
    11.  
    Manager:
    Code (CSharp):
    1.  
    2. public class LabManager : MonoBehaviour
    3. {
    4. public class CornStat
    5. {
    6. public int inventoryPlaceNum;
    7. public int sweet;
    8. public int juicy;
    9. public int size;
    10. public string quality;
    11. public int statPoint;
    12. }
    13. public CornStat[] cornStatArray;
    14.  
    15. public static LabManager Instance;
    16.  
    17. void Awake()
    18. {
    19. if (Instance == null)
    20. {
    21. Instance = this;
    22. }
    23. else if (Instance != this)
    24. {
    25. Destroy(gameObject);
    26. }
    27. DontDestroyOnLoad(gameObject);
    28. }
    29.  
    30. void Start()
    31. {
    32. cornStatArray = new CornStat[_maxInventorySpaceNumber];
    33. }
    34.  
    Thanks for helping
     
    Last edited: Jan 18, 2020
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609