how to make a level block/unblock
Hi everybody, I am Arkin160, but we don't care :) . I made a lot of research and I don't find what I need, that's why I ask the question here. I know the question was probably asked a lot of time, but i really need your help! I explain you the situation: I'm making a game in 2dand my menu is "in game":
I have 3 scripts; 2 in java et 1 in .txt and when you finish "level 1", there's a "1", which is in the .txt file (i called the .txt file "unlock level file.txt") with this script (I named it a.js but you can name it like you want).here is the a.js script:
#pragma strict
import System.IO;
var filePath = "/Users/Shared/Unity/game/Assets/unlock level file.txt";
var level : int;
function OnTriggerStay2D(other : Collider2D){
WriteFile(filePath);
}
function Start(){
WriteFile(filePath);
}
function WriteFile(filepathIncludingFileName : String)
{
var sw : StreamWriter = new StreamWriter(filepathIncludingFileName);
sw.WriteLine(level);
sw.Flush();
sw.Close();
}
and that's working, but the problem is now with the b.js script:
#pragma strict
var dataFile : TextAsset; //.txt file
var enter : GameObject; //trigger to load level x scene
var red_plateform : GameObject; //green or red background of the level to see if it's unlocked or not
var green_plateform : GameObject;
function Start() {
var returnChar = "\n"[0];
var dataLines = dataFile.text;
var buildDataPairs = new ArrayList();
for (var dataLine in dataLines) {
var dataPair = dataLine;
buildDataPairs.Add(dataPair);
enter.SetActive(false);
red_plateform.SetActive(true);
green_plateform.SetActive(false);
}
var dataPairs = buildDataPairs.ToArray();
Debug.Log(dataPairs[0]);
if(dataPairs[0] == 1){ // <----- the problem is here!
enter.SetActive(true);
red_plateform.SetActive(false);
green_plateform.SetActive(true);
}
}
Just one request, please use javascript because i can't really use c#, but if you can't do this in java and you know how to do this in c#, please comment all lignes (what you do and what's the point), I'll get you my biggest thank's. if you have an other method you can propose, I'm opened at all propositions I hope you have all understand. if you have any questions I answer relatively quickly because I check the web site every 2 hours. Thank's a lot people! big heart on you :)
Greeting
Arkin160
I'm using C#, but my guess would be that the comparison would need to be this unless you convert the text from the file to an integer:
if(dataPairs[0] == "1"){
On second thought: I'm not sure if your linewise reading is working. You did not say what your Debug.Log() is printing or if you inspected the value of dataPairs[0], but probably this link might also help:
http://forum.unity3d.com/threads/read-from-textasset-line-by-line.327422/
Answer by AurimasBlazulionis · Sep 14, 2016 at 08:42 AM
Your problem is that you compare string with int. You first need to convert that string to an integer. So instead of dataPairs[0]
you would write int.Parse(dataPairs[0])
(or just compare to "1"
as pointed out above).
But you also have some fundamental faults in your code too. The first one is path. You store the file in a constant location. To get the file stored in the correct and persistent path on any device, you will need to use Path.Combine (Application.PersistentDataPath, "whatever sub path you want")
instead of that constant string.
Also, you store all the data in a text file, which can be easily edited. I would suggest looking into binary serialization for the sake of enabling less tinkering for the players or PlayerPrefs for the sake of easiness for you.
Thank's a lot! Thank's Thank's thank's and thank's agen! it is so helpfull!
Your answer
Follow this Question
Related Questions
BCE0043 unexpected token: ")"? 1 Answer
Enemy keeps floating throw roof? 0 Answers
How to add a script to a parent object and not every individual child object. 1 Answer
Increase variable? 0 Answers