Search Unity

Question One class does't see the field of another class

Discussion in 'Scripting' started by farhatvf475, Jul 3, 2020.

  1. farhatvf475

    farhatvf475

    Joined:
    Dec 27, 2019
    Posts:
    5
    Code (CSharp):
    1. public class SaveSystem : MonoBehaviour
    2. {
    3.     [SerializeField] private String lvlName;
    4.     [SerializeField] private GameObject coin;
    5.     public LvlState lvlState;
    6.     [SerializeField] private Vector2[] conisPosition=new Vector2[3];
    7.     private void Awake()
    8.     {
    9.       //  if (PlayerPrefs.HasKey(lvlName))
    10.         {
    11.             Debug.Log(lvlState.disableCoins.Length);
    12.             lvlState = JsonUtility.FromJson<LvlState>(PlayerPrefs.GetString(lvlName));
    13.             CoinsSpawn();
    14.         }
    15.     }
    16.   public void CoinsSpawn()
    17.     {
    18.         for(int i = 0; i < 4; i++)
    19.         {
    20.             if (lvlState.disableCoins[i]==true) //NullReferenceException: Object reference not set to an instance of //an object
    21. //SaveSystem.CoinsSpawn ()
    22.             {
    23.              
    24.                 Instantiate(coin);
    25.             }
    26.         }
    27.     }
    28. }
    29.  
    30. [Serializable]
    31. public class LvlState
    32. {
    33.    [SerializeField]public bool[] disableCoins = new bool[3];
    34.  
    35. }
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Are we supposed to guess which class doesn't see which field?
    Do you mean the null reference comment you're referring to in line 20 of the SaveSystem class?
     
  3. farhatvf475

    farhatvf475

    Joined:
    Dec 27, 2019
    Posts:
    5
    Yep ,LvlState class, disableCoins field
     
  4. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    542
    Did you try to google the error message or debug the code?
     
  5. farhatvf475

    farhatvf475

    Joined:
    Dec 27, 2019
    Posts:
    5
    Yes im trying , it prints the length of disableCoins, but if put in a condition it gives "null" . line 20
     
  6. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    542
    You print the length in line 11, but in line 12 you override the lvlState variable with the json value, maybe the json call returns null
     
  7. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    1. Did you assign the LvlState reference in the SaveSystem's inspector? If yes...
    2. Did you assign any values in the LvlState's inspector?
    One other thing I'm noticing is that your for-loop will cause an IndexOutOfBoundeException. It will loop 4 times, but your array has only 3 values (assuming you didn't add any more in the inspector).
    Instead of using a set number to loop by, use the length of the array:
    for(int i = 0; i < lvlState.disableCoins.Length; i++)
     
  8. farhatvf475

    farhatvf475

    Joined:
    Dec 27, 2019
    Posts:
    5
    Yes,you'r right.Big thanks