Hey guys. So I’m working on a really simple save/load game system for my game. I followed the Brackeys’ tutorial (
) but he uses the system on some buttons, while I want to use it on some triggers.
My system would work just like Brackeys, however, instead of clicking 2 buttons, you would have to enter a box collider with the character, press the Jump button, and there, the save / load system would be triggered.
It’s not that much of a change from what Brackeys did, however, when I try to do it, Unity says “Assets\Scripts\SavePoint.cs(26,13): error CS0120: An object reference is required for the non-static field, method, or property ‘PlayerMovement.SavePlayer()’”
So I googled for a few hours, and no luck. Most of the answers I found were saying that I have to reference the method with the class name, but I’m already doing so…
Anyways, here is my script. I’m still very new to Unity and C# so please don’t pay too much attention to the mess it is. I’ll try to explain how it would work:
They are 4 scripts.
1st script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SavePoint : MonoBehaviour
{
bool colliding;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
colliding = true;
}
}
void OnTriggerExit2D(Collider2D other)
{
colliding = false;
}
void Update()
{
if (colliding && Input.GetButtonDown("Jump"))
{
PlayerMovement.SavePlayer();
}
}
}
SavePoint is basically the script attached to the gameObject where, when the character is inside its Box Collider 2D, if he Jumps, the SavePlayer() from the PlayerMovement script, will be called. Here is where the error occurs, being " An object reference is required for the nonstatic field, method, or property ‘PlayerMovement.SavePlayer()’.
2nd script:
public void SavePlayer()
{
SaveSystem.SavePlayer(this);
}
public void LoadPlayer()
{
PlayerData data = SaveSystem.LoadPlayer();
lastLevelCleared = data.level;
curHealth = data.health;
vidasExtra = data.lives;
}
here we have the SavePlayer from the PlayerMovement script. It just calls the SaveSystem.SavePlayer() and uses ‘this’ to save the “stats” from PlayerMovement. The script is much longer, but this is the only part we need to look at. SavePlayer() and LoadPlayer() are both outside of Start() and Update()
3rd script:
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem //La hago static para evitar que se creen multiples versiones del guardado porque las clases static no pueden instanciarse.
{
public static void SavePlayer (PlayerMovement player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.formatter"; //le puedo poner la terminación que quiera para aumentar la seguridad, pero sigue siendo el propio archivo de guardado
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData data = new PlayerData(player);
//Ahora tenemos todos los datos formateados tal y como queriamos, asi que lo metemos todo a un file
formatter.Serialize(stream, data);
//En cuanto acabemos de escribir datos en el file, lo cerramos
stream.Close();
}
public static PlayerData LoadPlayer()
{
string path = Application.persistentDataPath + "/player.formatter";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData; //con esto no transforma de binario al anterior formato
stream.Close();
return data;
}
else
{
Debug.LogError("No existe partida guardada en " + path); //Quiza en vez de esto, hacer que salga un mensaje sobre el personaje que diga que no hay guardado?
return null;
}
}
}
this is the SaveSystem script. Ignore the spanish comments, they are all explanations for myself. After this, we would get to the PlayerData script.
4th script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PlayerData
{
public int level;
public float health;
public int lives;
public PlayerData (PlayerMovement player)
{
level = player.lastLevelCleared;
health = player.curHealth;
lives = player.vidasExtra;
}
}
Last script, where the player data would be stored.
I have no idea of where the problem is. I’m really lost, and I would really appreciate any help at all, because I know it can’t be too hard to solve it, but I don’t really have much time left before having to submit the game (30 hours) and I would like to have the saving system implemented. Thanks in advance for your time!!