I get this error when pressing the save button and I do not know what is the problem ?
Error console:
script save and load:
calendario script:
I get this error when pressing the save button and I do not know what is the problem ?
Error console:
I think the problem is because you try to serialize a Text MonoBhevaiour, not sure though as I never try to serialize a monobehaviour (Fecha) in the first place (which is the warning you get), so it might just as wel the serializable attribute on top of that causing some errors.
What should I do then?
Remove the MonoBehaviour base class from Fecha and it should work. Then if you need that data in a separate object as MonoBehaviour, create a separate object for that and assign the values when deserializing.
Error cosole: The class named ‘Fecha’ is not derived from MonoBehaviour or ScriptableObject!
Console with MonoBehaviour:
You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor()
Fecha:.ctor()
SaveLoad:Save() (at Assets/Scripts/SaveLoad.cs:27)
UnityEngine.EventSystems.EventSystem:Update()
As an example:
SaveLoad class:
using Assets;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
[RequireComponent(typeof(FechaBehaviour))]
public class SaveLoad : MonoBehaviour
{
// Use this for initialization
void Start()
{
Save();
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyUp(KeyCode.Space))
{
Load();
}
}
void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "save.dat");
Fecha fecha = new Fecha
{
dia = 20f
};
bf.Serialize(file, fecha);
file.Close();
}
void Load()
{
if(File.Exists(Application.persistentDataPath + "save.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "save.dat", FileMode.Open);
var fecha = (Fecha)bf.Deserialize(file);
file.Close();
var fechaBehaviour = GetComponent<FechaBehaviour>();
fechaBehaviour.dia = fecha.dia;
}
}
}
Fecha class:
using System;
namespace Assets
{
[Serializable]
public class Fecha
{
public float dia;
}
}
Fecha Behaviour:
using UnityEngine;
namespace Assets
{
public class FechaBehaviour : MonoBehaviour
{
public float dia;
private void Update()
{
if (dia != 0.0f)
{
Debug.Log(dia);
}
}
}
}
Sorry for the formatting, Unity forums seem to hate me ones again.
Oh thanks, but of course I would like to just change “fecha” because I would like to save other data as prefabs etc
I just want to know that changing the fecha to save it
okay i change the system to a player prefs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SaveLoad : MonoBehaviour {
public GameObject [ ] LocalizadorDeObjetos;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void Save () {
// Busquueda de objetos
LocalizadorDeObjetos = GameObject.FindGameObjectsWithTag (“Objeto”);
// Guardado de datos de objetos
PlayerPrefs.SetFloat(“x”,LocalizadorDeObjetos);
// PlayerPrefs.SetFloat(“y”,ObjetosComprados.transform.position.y);
// PlayerPrefs.SetFloat(“z”,ObjetosComprados.transform.position.z);
// PlayerPrefs.SetFloat (“”,transform.position.x)
}
public void Load () {
}
}
but the problem now is how to set float all objects