Saveing and Loading Players with binary formattor

Because I’m Korean, I can’t speak English well. Please understand.
And I use this Q&A first time.
Thank you for your understanding even if it is strange.

Hello, I want to make a saVe system but I keep getting an error. NullReferanceExeption.
I spend 3days but I can’t fix it. plz help me.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[SerializeField]
public class AllPlayersData
{
    public PlayerSaveData[] playerSaveDatas;

    public AllPlayersData(PlayerSaveData[] playerSaveDatas){
        this.playerSaveDatas = playerSaveDatas;
    }
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Player : MonoBehaviour
{
    
    public int HealthPoint = 1;
    public float AttackPoint = 0.5f; 

    public void SavePlayer(){
        //PlayerSaveManager.Save(GameObject.FindObjectsOfType<Player>());
        // PlayerSaveData save = new PlayerSaveData(this);
        // save.HP = HealthPoint;
        // save.AP = AttackPoint;
        // save.x = transform.position.x;
        // save.y= transform.position.y;
        // save.z = transform.position.z;

        PlayerSaveManager.Save(GameObject.FindObjectsOfType<Player>());
    }

    public void LoadPlayer(){
        AllPlayersData save = PlayerSaveManager.Load();
        Player player = GameObject.FindObjectOfType<Player>();

        //여기가 Vector3 pos파트임.
        for(int i=0; i< save.playerSaveDatas.Length; i++){
            Player[] players = new Player[save.playerSaveDatas.Length];

            Vector3 position;
            position.x = save.playerSaveDatas*.position[0];*

position.y = save.playerSaveDatas*.position[1];*
position.z = save.playerSaveDatas*.position[2];*
}
}

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//얘가 Data로 치면 된다.
[System.Serializable]
public class PlayerSaveData
{
public int HP;
public float AP;

public float[] position;
public float x;
public float y;
public float z;

public PlayerSaveData(Player B){
HP = B.HealthPoint;
AP = B.AttackPoint;

x = B.transform.position.x;
y = B.transform.position.y;
z = B.transform.position.z;

position[0] = x;
position[1] = y;
position[2] = z;
}

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;

// 참고 자료입니다.
//이게 SaveSystem

public static class PlayerSaveManager
{
public static void Save(Player[] players){
BinaryFormatter formatter = new BinaryFormatter();
string path = Path.Combine(Application.dataPath, "Fuck You Save Player.bin ");
FileStream stream =File.Create(path);

//교체 사항
PlayerSaveData[] playerSaveDatas = new PlayerSaveData[players.Length];
for(int i = 0; i<players.Length; ++i){
playerSaveDatas = new PlayerSaveData(players*);*
}

AllPlayersData data = new AllPlayersData(playerSaveDatas);

Debug.Log(data);

formatter.Serialize(stream, data);
stream.Close();
}

public static AllPlayersData Load(){
try{
BinaryFormatter formatter = new BinaryFormatter();
string path = Path.Combine(Application.dataPath, “Fuck You Save Player.bin”);
FileStream stream = File.OpenRead(path);
AllPlayersData data = (AllPlayersData)formatter.Deserialize(stream);
stream.Close();
return data;
}
catch(Exception e){
Debug.Log(e.Message);
return default;

}
}

}

It would help if we knew the number of the line where the error happens. By reading the code, I can think of these lines:

Line 36 (FindObjectsOfType) can return null if no GameObjects of type ‘Player’ is found. This can lead to problems later.

Line 81 (accessing position[0]) can definitely lead to NullReferenceException since you haven’t created the array in the first place.

position = new float[3];
position[0] = x;
position[1] = y;
position[2] = z;

Please let me know if it helped or not. Good luck.

Check out Brackeys Tutorial on making a Saving And Loading System

SAVE & LOAD SYSTEM in Unity