Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Danor · Sep 09, 2014 at 07:03 PM · c#game

Saveing then loading the last unlocked level with continue button

I'm trying to save the game everytime the player reaches a portal and then when the game is stopped and when player wants to play again he can press the continue button on the startmenu to start at the level he last unlocked.

here is the tutorial: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

here is one of the scripts on the portal that should save the game:

 using UnityEngine;
 using System.Collections;
 
 public class Saving : MonoBehaviour {
     void OnTriggerEnter(Collider other) {
         if(other.gameObject.CompareTag("Player")){
             GameControl save = GetComponent<GameControl>();
             save.Save();
             }
         }
     }

gives no errors.

here is the script for the continue button:

 using UnityEngine;
 using System.Collections;
 
 public class ContinueCS : MonoBehaviour {
 
 void OnMouseUp(){
     GameControl.control.Load();
     int levelcount =  GameControl.control.levelcount;
 
     if(levelcount == 0){
         Application.LoadLevel("Level1");
     }
     else
     {
         Application.LoadLevel(levelcount);
     }
 }
 }

no errors

and here is the script based on the tutorial:

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class GameControl : MonoBehaviour {
     public static GameControl control;
 
     public float score;
     public int levelcount;
 
     void Awake () {
         if(control == null)
         {
             DontDestroyOnLoad(gameObject);
             control = this;
         }
         else if(control != this)
         {
             Destroy(gameObject);
         }
     }
 
     public void Save()
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
 
         PlayerData data = new PlayerData();
         data.score = score;
         data.levelcount = levelcount;
 
         bf.Serialize(file, data);
         file.Close();
     }
     public void Load()
     {
         if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
             PlayerData data = (PlayerData)bf.Deserialize(file);
             file.Close();
 
             data.score = score;
             data.levelcount = levelcount;
 
         }
     }
 }
 [Serializable]
 class PlayerData
 {
     public float score;
     public int levelcount;
 }

It would be great if someone could tell me what i'm missing or doing wrong. Thanks in advance

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CyberWolf · Sep 09, 2014 at 09:57 PM

Its pretty late, but just at a glance, shouldn't the:

 data.score = score;
 data.levelcount = levelcount;

be:

 score = data.score;
 levelcount = data.levelcount;

in the load function?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Danor · Sep 10, 2014 at 01:42 PM 0
Share

you are right, i changed it but it still doesn't work, any other idea's?

avatar image CyberWolf · Sep 10, 2014 at 08:24 PM 0
Share

I am also not so sure about:

 GameControl save = GetComponent<GameControl>();
 save.Save();

Because if both of the scripts are not connected to the same Gameobject I'm not sure if GetComponent will return what you are looking for.

Have you tried just calling:

 GameControl.control.Save();

?

Aside from that it is hard to tell without knowing the whole picture. (For example, is it 2D or 3D? Because in 2D you need to use "OnTriggerEnter2D" and so on.)

avatar image CyberWolf · Sep 11, 2014 at 05:11 AM 0
Share

You can try adding debug messages to various parts of the code to see if it even executes. (for example you can check if the trigger works)

http://docs.unity3d.com/ScriptReference/Debug.Log.html

Also you should check if the file itself is actually created.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

how do i Disable and Enable buttons? 2 Answers

How to access variables from an object based on location? C# (Pic inside) 1 Answer

Character ShopSave 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges