Save and Load Game – Unity3D (C#)

Save and Load Game – Unity3D (C#)

People thinks that save and load functions are a little bit complicated in game development. We will use 2 types of game saving and game loading in Unity3D and you will get that it is not as complex as you think. We use PleyerPrefs and persistentDataPath.


Using PlayerPrefs:

You can save and load 3 types of variables: int, float and string. If you want to save or load player name, score, time etc., PlayerPrefs is the easiest way. Just give a name and a  value across it. Lets try to save and load player score. It is compitable with web player.

In code above, we save score as Score with PlayerPrefs.SetInt function. We load Score and assign it to savedScore with PlayerPrefs.GetInt function. Now, when you press play, it saves score with S key and loades score with L key. Every time you save and load, you can see saved and loaded score in console with Debug.Log function.


Using persistentDataPath:

In this section, we work with I/O files. We can use multiple values with persistentDataPath. For example, create a Player class with health and name. we will save and load these values.

Firstly, we need to add these 3 libraries.

In this code, we create a file named FileName.dat and we save our class data. You need to add [Serializable] before your class.

As I said, you can save int, float and string type values. That doesn’t mean you can’t save color, position etc. If your player position is Vector3, you can save it with float values such as position.x, position.y, position.z. Also you can save color with its name and call it in load with name.


©Coffee Break Codes – Save and Load Game – Unity3D (C#)

Leave a comment

Your email address will not be published. Required fields are marked *

18 thoughts on “Save and Load Game – Unity3D (C#)

  • zak

    HI! tried your tutorial keep getting:

    error CS0246: The type or namespace name `ObjData’ could not be found. Are you missing a using directive or an assembly reference?

  • vay tien ngan hang

    Bạn đang có nhu cầu vay tiền mà chẳng
    thể đề xuất giúp đỡ từ bạn bè
    hay người nhà . Bạn với thể tới vay
    tiền banktop sẽ đáp ứng đúng lúc nhu cầu
    cần vay vốn nhà băng của bạn sở hữu sản phẩm cho
    vay dùng trả góp không cần tài sản bảo đảm.

    Vay tiền banktop cho vay các loại vay
    tiền theo bảng lương banktop, vay tiền theo cavet xe banktop, vay tin chấp
    banktop, vay tiều dùng banktop. Liên hệ: 0909 023
    278, 144 Cộng Hòa Phường 12 Quận Tân Bình, TP HCM
    ko cần thế chấp tài sản, giấy tờ tiện lợi , tương trợ
    tận nơi khách bắt buộc , Định mức vay cao nhất, giải đáp và làm cho
    giấy tờ miễn phí, Hạn mức trả góp dài nhất
    kiên cố sự góp sức của banktop sẽ kịp thời tương trợ cho thành công của bạn

  • gamefly

    Very nice post. I just stumbled upon your blog and
    wanted to say that I’ve truly enjoyed surfing around your weblog posts.
    After all I’ll be subscribing on your feed and I am hoping you write once more soon!

  • gamefly free trial

    Hi there this is kind of of off topic but I was wanting
    to know if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding skills
    so I wanted to get advice from someone with experience.

    Any help would be enormously appreciated!

  • gamefly free trial

    I do not know if it’s just me or if perhaps everybody else encountering issues with your blog.
    It appears like some of the text on your posts are running off the screen. Can somebody else please comment and let me know if this is happening to them
    as well? This could be a problem with my browser because I’ve had this happen before.
    Many thanks