Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

I need help to save and load

Discussion in 'General Discussion' started by FloatingEagle, Feb 6, 2020.

  1. FloatingEagle

    FloatingEagle

    Joined:
    Feb 6, 2020
    Posts:
    2
    Im pretty new to unity and i wanna make a save and load system to my mobile game. This is the code:


    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;

    public class IdleScript : MonoBehaviour
    {

    public Text coinsText;
    public Text clickUpgrade1Text;
    public double coins;
    public double coinsClickValue;
    public double clickUpgrade1Cost;
    public int clickUpgrade1Level;


    public void Start(){
    coinsClickValue = 1;
    clickUpgrade1Cost = 10;
    }



    public void Update(){

    coinsText.text = coins.ToString("F0") + "$";
    clickUpgrade1Text.text = "Click Upgrade 1\nCost: " + clickUpgrade1Cost + " coins\nPower: +1 Click\nLevel: " + clickUpgrade1Level;
    }

    public void Click(){
    coins += coinsClickValue;
    }

    public void BuyClickUpgrade1(){
    if (coins >= clickUpgrade1Cost){
    clickUpgrade1Level++;
    coins -= clickUpgrade1Cost;
    clickUpgrade1Cost += 100;
    coinsClickValue++;
    }
    }
    }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,022
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    Antypodish and Ryiah like this.
  4. FloatingEagle

    FloatingEagle

    Joined:
    Feb 6, 2020
    Posts:
    2
  5. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,806
    Why are you using doubles in the first place?
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Convert the double to a string. Save it. Then reload it from a string and convert it back.

    Saving basically looks like this, regardless of the data:
    1. Gather the data to be saved
    2. Convert the data to a string (serialize)
    3. Write the string to a save location
    Also check out this tutorial. https://learn.unity.com/tutorial/persistence-saving-and-loading-data

    (Here is the link without the new learn section BS.)
     
    JamesArndt and Ryiah like this.