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 /
avatar image
1
Question by Terena · Sep 06, 2013 at 07:34 PM · importsaveloadexport

saving data in arrays with c#..how ? ...

Hi, I'm currently building a game. To make it easier for me to design levels, I created a map builder for my self first. So, it should save the prefabs I Instatiated to map and its coordiantes with separetes arrays.

So I want to save these datas in the array so I can use them later in my game later on when ever I want.

Problem is how ? I never try to save anything in unity before.

Thank you.

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
2
Best Answer

Answer by legion_44 · Sep 06, 2013 at 08:51 PM

So, here you have the array and a list. The difference is you can't resize array in runtime (you can do this with list).

 //Writed by Paul Ignasiak
 //C#
 
 List<Transform> listOfTransforms = new List<Transform>();
 Transform[] arrayOfTransforms;

If you want to add something to array or list, just write

 //Writted by Paul Ignasiak
 
 Transform myTransform;
 
 void myMethod()
 {
  listOfTransforms.Add(myTransform);
  //or
  arrayOfTransforms[index] = myTransform;
  //index is value thats specificates what "place" we should assign myTransform to.
 }

If you want to read the position:

 Vector3 pos;
 
 void myMethod()
 {
 pos = listOfTransforms[index].position;
 //or
 pos = arrayOfTransforms[index].position;
 }

So, if you mean saving to file, you have to use the System.IO library. This is an example how you can use it to save names of Transforms from array.

 using UnityEngine;
     using System.Collections;
     using System.IO;
     
     public Transform[] myTransforms;
     
     void saveAll()
     {
      StreamWriter sw = new StreamWriter(@"C:\tmp\text.txt");
      foreach(Transform t in myTransforms)
      {
       sw.WriteLine("" + t.name);
      }
      sw.Close();
     }

Of cource you can modify it to your preferences, and change C:\tmp\text.txt to what you want but you have to be warned of writing file in directory that no exists. To prevent that, use this code:

     if(!Directory.Exists(@"directory"))
     {
     Directory.CreateDirectory(@"directory");
     }
 
 //The '@' symbol before the string is used to gain posibility of use "\" in string.

If you want to read them, use following code:

 Transform[] myTransforms;
 
 void loadAll()
 {
 StreamReader sr = new StreamReader(@"C:\tmp\text.txt")
 
  for(int i = 0; i < myTransforms.Lenght; i++)
  {
   myTransforms[i].name = sr.ReadLine();
  }
 }

Hope that helps

Paul

Comment
Add comment · Show 4 · 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 Terena · Sep 07, 2013 at 08:07 AM 0
Share

Thanks but, I'm asking how to save them. I'm asking saving the arrays to use save / load or import / export

avatar image Sajidfarooq · Sep 07, 2013 at 08:09 AM 0
Share

@Terena: Next time, please use a "comment" rather than an "answer" to reply to an existing question. Converted it to comment for you this time.

avatar image legion_44 · Sep 07, 2013 at 08:17 AM 0
Share

@Terena: Updated answer :). Hope that helps.

avatar image Terena · Sep 09, 2013 at 06:43 AM 0
Share

Thanks alot

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Import/Export of save file string 0 Answers

Level exporting (with video example) 1 Answer

Save and load a class 0 Answers

I need to save and load GameObjects. How do I do this? 3 Answers

Save game support, how do I load audio in a sertain time frame? 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