Reputation: 1
i am trying to add a save system to my android game where the data gets saved in a text file after completing the level the scene
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.IO;
public class CollisionHandler : MonoBehaviour
{
Rigidbody2D playerRb;
public GameObject targ;
// Start is called before the first frame update
void Start()
{
Debug.Log("started");
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter2D(Collision2D coll)
{
Debug.Log("collided");
if (coll.gameObject.tag == "DANGER")
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
else if (coll.gameObject.tag == "Finish")
{
SetVar();
SceneManager.LoadScene("Menu");
}
}
public void SetVar()
{
Scene currentScene = SceneManager.GetActiveScene ();
string sceneName = currentScene.name;
if (sceneName == "Level1")
{
File.WriteAllText("Assets/texts/lvl1.txt", "true");
}
else if (sceneName == "Level2")
{
File.WriteAllText("Assets/texts/lvl2.txt", "true");
}
else if (sceneName == "Level3")
{
File.WriteAllText("Assets/texts/lvl3.txt", "true");
}
else if (sceneName == "Level4")
{
File.WriteAllText("Assets/texts/lvl4.txt", "true");
}
else if (sceneName == "Level5")
{
File.WriteAllText("Assets/texts/lvl5.txt", "true");
}
else if (sceneName == "Level6")
{
File.WriteAllText("Assets/texts/lvl6.txt", "true");
}
}
}
When i tried this in the editor the scenes loaded as usual and the data got saved in the file. I tried changing the write permission to External but it still didn't work. when i built the game as an executable it worked like it should.
Upvotes: 0
Views: 63
Reputation: 90639
Most probably because you get a DirectoryNotFoundException
.
File.WriteAllText("Assets/texts/lvl1.txt", "true");
works within the Unity Editor since here you have direct access to your file system.
Once you build your application the entire Unity project is compiled and packed according to your target platform and Assets
first of all is not a plain directory anymore and second is read-only.
You should rework this and rather write into Application.persistentDataPath
Btw if this is about unlocking levels instead of actually creating a new file for each level I would rather simply store the index of the highest level that is unlocked.
public void SetVar()
{
var currentScene = SceneManager.GetActiveScene();
// e.g. "Level5"
var currentSceneName = currentScene.name;
// cut away the "Level" part of the name -> only keep index
// e.g. "5"
currentSceneName.Replace("Level", "", StringComparison.InvariantCultureIgnoreCase);
// if can not be parsed to an index -> ignore
if (!int.TryParse(currentSceneName, out var currentIndex))
{
return;
}
// get the levels folder and file path
var levelsFolder = Path.Combine(Application.persistentDataPath, "levels");
var levelsFile = Path.Combine(levelsFolder, "passed.txt");
// default index if no save file exists so far
var lastPassedIndex = -1;
if (File.Exists(levelsFile))
{
// otherwise read the file and overwrite "lastPassedIndex"
var lastPassedText = File.ReadAllText(levelsFile);
int.TryParse(lastPassedText, out lastPassedIndex);
}
// is this level actually higher than the currently highest?
if (currentIndex > lastPassedIndex)
{
// if folder doesn't exist
if (!Directory.Exists(levelsFolder))
{
// create it
Directory.CreateDirectory(levelsFolder);
}
// store the current index
File.WriteAllText(levelsFile, currentIndex.ToString());
}
}
Upvotes: 0