-1

I was wrote a script that combines whale characters to create a new character. However, it is difficult to save and load data from a list of names, levels and locations of the newly created characters.

Whale data does not seem to be stored and loaded when the game is played.

I'd really thanks it if you could tell me where's wrong.

using system;
using System.Collections;
using System.Collections.General;
using system.IO;
using LitJson;
using UnityEngine;
using Random = UnityEngine.Random;

[System.Serializable]
Public class WhaleData
{
    Public in dataLevel{get; set;// whale level
    public Vector3 dataMousePosition{get; set;//the whale location
    public string dataName{get; set;} //whale name
}

Public class Whale: MonoBehaviour
{
    public int level;
    Private pool isDrag;
    Public GameObject NextPrepab;
    Private Vector3 mousePosition;
    Public WhaleData whaleData;
    Private List[WhaleData] WhaleDatas = new List[WhaleData](); 
    private pool IsSave; //bool value to check for stored data

    void start()
    {
        IsSave = PlayerPrefs.HasKey ("0_name"); //saved_level Check if a key value named //saved_level exists
        //initialize all data values if no save data exists
        If (!IsSave)
        {
            Debug.Log ("No data stored).");
            PlayerPrefs.DeleteAll();
        }

        //recall value if save data exists
        else
        {
            Debug.Log ("Stored Data").");
            LoadData();
        }
    }

    Private void Update ()
    {
        SaveData();
        LoadData();
    }

    private void onMouseDown()
    {
        isDrag = true;
    }

    private void OnMouseDrag()
    {
        if (isDrag)
        {
            mousePosition.x = Input.mousePosition.x;
            mousePosition.y = input.mousePositionY;
            mousePosition.z = 10;

            //change the mouse coordinates to the screen to world and set the position of this object
            gameObject.transform.position = Camera.main.ScreenToWorldPoint(mousePosition);
            // store the location of the whale in the whalData.dataMousePosition= gameObject.transform.position; //
        }
    }

    private void onMouseUp()
    {
        //OverlapSphere : Return from the specified location to the collider array in contact within range
        var colliders = Physics.OverlapSphere (transform.position, 0.1f); 

        foreach (var col in colliders) // arrangement of surrounding contacts
        {
            If (name!= col. gameObject.name) //if the name is different, go
            {
                If (level==col.gameObject.GetComponent[Whale>().level) //If the level of contact with the level stored in me is the same,
                {
                    whatData.dataLevel = col.gameObject.GetComponent[Whale>().level; // store the level of the whale in the whalData class
                    var newWhale = Instantiate(NextPrepab, transform.position, quaternion.identity);
                    newWhale.name = Random.Range (0f, 200f).ToString(); //name is random. 
                    dateData.dataName = name;// store the name of the whale in the whalData class
                    SaveData();

                    print(col.gameObject.name);
                    print(gameObject.name);
                    whatDatas.Add(whaleData);

                    Destroy (col.gameObject);
                    Destroy (gameObject);

                    // delete if there is any whale information with the same name as Whale's name that will disappear from the dateDatas list (use a simple calculation expression of the unknown method)
                    if (whaleDatas.Find(whaleData=>whaleData.dataName=colgameObject.GetComponent[Whale>(.name).dataName==colgameObject.GetComponent[Whale>(.name)
                    {
                        whaleDatasRemove (col.gameObject.GetComponent[Whale>(whaleData));
                        Destroy (col.gameObject);
                        Destroy (gameObject);
                    }
                    else
                    {
                        break;
                    }

                    break;
                }
            }
        } 

        isDrag = false;
    }

    Public static void setVector3 (string key, Vector3 value)
    {
        PlayerPrefs.SetFloat (key + "X", value.x);
        PlayerPrefs.SetFloat (key + "Y", value.y);
        PlayerPrefs.SetFloat (key + "Z", value.z);

    }

    Public static Vector3 GetVector3 (string key)
    {
        Vector3 v3 = Vector3.zero;
        v3.x = PlayerPrefs.GetFloat (key + "X");
        v3.y = PlayerPrefs.GetFloat (key + "Y");
        v3.z = PlayerPrefs.GetFloat (key + "Z");
        return v3;
    }
    private void saveData()
    {
        for (int i=0; i <whaleDatas.Count; i++)
        {
            PlayerPrefs.SetString(i+"_name",whaleDatas[i‐dataName));
            PlayerPrefs.SetInt(i+"_level",whaleDatas[i‐dataLevel));
            Vector3 value = whatDatas[i].dataMousePosition;
            string key = i + "_position";
            SetVector3 (key,value);
            PlayerPrefs.Save();
            Debug.Log ("Saved");
        }
    }
    private void LoadData()
    {
        for(int i=0; i <whaleDatas.Count; i++)
        {
            whaleDatas[i].dataName = PlayerPrefs.GetString(i+"_name");
            whaleDatas[i].dataLevel = PlayerPrefs.GetInt(i+"_level");
            string key = i + "_position";
            whaleDatas[i].dataMousePosition = GetVector3(key);
        }
    }
}
Laon
  • 3
  • 2
  • 2
    add debugging messages, show what you check for what, you got, etc, look in your player prefs settings file... – BugFinder Nov 17 '19 at 20:57
  • In general: Avoid using `PlayerPrefs` for storing sensitive data and user progress .. rather store to a server or file and use e.g. XML or JSON... – derHugo Nov 18 '19 at 06:20
  • Also in general: Please post your code as is and fix typos here ... e.g. `Start`, `OnMouseDown`, `OnMouseUp` should all be capital in order to be called by Unity ... `public` and `private` however should not .. `Public` and `Private` don't exist as keyword ... – derHugo Nov 18 '19 at 06:44

1 Answers1

0

Your code is actually broken in term of design.

First PlayerPref doesnt support boolean save by default.

IsSave = PlayerPrefs.HasKey //this will always return false.

thats why everytime you start the game all your saved data is deleted. You can try this work around to save and load a boolean type How to save bool to PlayerPrefs Unity

Now, How on earth you decided to save and load data at update function?! , this is terrible and has huge performance cost if your data is huge. you just cant do it this way.

Instead, try to make events that store the data and load it instead, like once player enter a trigger box , the data is saved, and ofcourse you dont need to reload it if its saved , bec why you would have to load the data after its being saved?!!

Once again, dont read and write at Update Function like this all the time, this is bad design .

VectorX
  • 657
  • 5
  • 12
  • Agreed it probably would be enough e.g. in `onMouseUp` ...btw if you want this to be called by Unity it should be a capital `O` for `OnMouseDown` and `OnMouseUp` ... – derHugo Nov 18 '19 at 06:24