• Advertisement
Sign in to follow this  

SaveGameManager.Instance.SaveableObjects.Add(this); does not add to the list

This topic is 419 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i got 3 script for my save and load object which are SaveableObject script, SpecificObject script and SaveGameManager script. The Specific Object is inherited from the saveableobject. The problem that i facing now is my Saveable object does not add to my saveable object list i dont know what is the problem for this occuring. When i attach my specificObject script into the gameObject.It does not add to my SavebleObject list. SaveGameManager.Instance.SaveableObjects.Add(this);  does not add my Saveable object list into the game. Can someone tell me what i went wrong. Appreciate that.

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


enum ObjectType { mario, bird, rabbit }

public abstract class SaveableObject : MonoBehaviour
{
    
    protected string save;

    [SerializeField]
    private ObjectType objectType;

   public void Start()
    {
        
        SaveGameManager.Instance.SaveableObjects.Add(this);
        
       // PlayerPrefs.SetInt("Health", 30);
    }

    public virtual void Save(int id)
    {

        PlayerPrefs.SetString(SceneManager.GetActiveScene().ToString()+ "-" + id.ToString(),objectType +"_" + transform.position.ToString() + "_" + transform.localScale + "_" + transform.localRotation +save);
      //  int health = PlayerPrefs.GetInt("Health");
    }

    public virtual void Load(string[]values)
    {
        transform.localPosition = SaveGameManager.Instance.StringToVector(values[1]);
        transform.localScale = SaveGameManager.Instance.StringToVector(values[2]);
        transform.localRotation = SaveGameManager.Instance.StringToQuaternion(values[3]);
       // int health = PlayerPrefs.GetInt("Health");
    }

    public void DestroySaveable()
    {
        SaveGameManager.Instance.SaveableObjects.Remove(this);
        Destroy(gameObject);
    }

    
} 

First one is my SaveableObject Script

Second is my SaveGameManager Script

Third is my Specific Object Script

public class SaveGameManager : MonoBehaviour {

    private static SaveGameManager instance;

    public  List<SaveableObject> SaveableObjects { get; private set; } 

    public  static SaveGameManager Instance
    {
        get
        {   if(instance == null)
            {
                instance = GameObject.FindObjectOfType<SaveGameManager>();
            }
            return instance;
        }

    }

    void Awake ()
    {
        SaveableObjects = new List<SaveableObject>();
	}
	
    public void Save()
    {
        PlayerPrefs.SetFloat(SceneManager.GetActiveScene().ToString(), SaveableObjects.Count);
        for(int i = 0; i <SaveableObjects.Count; i++)
        {
            SaveableObjects[i].Save(i);
        }
    }
	
    public void Load()
    {
        foreach(SaveableObject obj  in SaveableObjects)
        {
            if(obj !=null)
            {
                Destroy(obj.gameObject);
            }
        }

        SaveableObjects.Clear();

        int objectCount = PlayerPrefs.GetInt(SceneManager.GetActiveScene().ToString());

        for(int i= 0; i< objectCount; i++)
        {
            string[] value = PlayerPrefs.GetString(SceneManager.GetActiveScene().ToString() +"-" + i.ToString()).Split('_');
            GameObject tmp = null;
            switch (value[0])
            {
                case "Mario":
                    tmp = Instantiate(Resources.Load("mario") as GameObject);
                    break;
                case "Bird":
                    tmp =  Instantiate(Resources.Load("bird") as GameObject);
                    break;
                case "Rabbit":
                    tmp = Instantiate(Resources.Load("rabbit") as GameObject);
                    break;
            }
            
                   if(tmp !=null)
            {
                tmp.GetComponent<SaveableObject>().Load(value);
            }
           


        }
    }

    public Vector3 StringToVector(string value)
    {

        value = value.Trim(new char[] {'(',')' });

        value = value.Replace("", "");

        string[] pos = value.Split(',');

        return new Vector3(float.Parse(pos[0]), float.Parse(pos[1]),float.Parse(pos[2]));
    }

    public Quaternion StringToQuaternion(string value)
    {
        value = value.Trim(new char[] { '(', ')' });

        value = value.Replace("", "");

        string[] pos = value.Split(',');

        return new Quaternion(float.Parse(pos[0]), float.Parse(pos[1]), float.Parse(pos[2]),float.Parse(pos[3]));
    }
	
}

using UnityEngine;
using System.Collections;

public class SpecificObject : SaveableObject {

    //private float speed;
    [SerializeField]
    private int health;

    private void Update()
    {

    }



    public override void Save(int id)
    {
        save = health.ToString();
        base.Save(id);
    }

    public override void Load(string[] values)
    {
        health = int.Parse(values[4]);
        base.Load(values);
    }
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement