Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Arkins_prod_studio · Sep 14, 2016 at 08:31 AM · javascriptlevel select

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":

alt text

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

capture-decran-2016-09-13-a-084228.png (475.8 kB)
Comment
Add comment · Show 1
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 ScaniX · Sep 14, 2016 at 08:32 AM 0
Share

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/

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · 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 Arkins_prod_studio · Sep 14, 2016 at 06:44 PM 0
Share

Thank's a lot! Thank's Thank's thank's and thank's agen! it is so helpfull!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

Type in certain URL, Open UI image. {Input Field} 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