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
0
Question by Adge · May 12, 2014 at 06:44 AM · instantiatearraypositionsaveloading

How to save/load the Y-Coordinates of Instantiated Objects

I have a grid of cubes that was too large for me to simply copy and paste. In order to make it, I needed to array a prefab using a for loop. These cubes are then translated up and down by the player using Raycasting. What I am trying to do is re-read the array, making a CSV of the Y-values with their changes. They don't move along any other axis, so I don't need to save them.

The intention is that later, the player can load their CSV and have the cubes return to the saved values.

This is the code I have to create the array, in case their is a better way to create it that would allow this functionality. As it stands, it saves the values, however this is only when everything loads.

 using UnityEngine;
 using System.Collections;
 using System.IO;
 
 public class FloorArray : MonoBehaviour{
 
     public GameObject defObject;
     GameObject defObject2;    
 
     public float xVal = -17.14359f;
     public float yVal = -1.830479f;
     public float zVal = -1.685268f;   
 
     //Variables used to save the coordinate values
     public string[] arrVector1 = new string[150]; 
     public string arrVector2;
     public Vector3 Vector;
     float fArr;
 
 void Start () {
         defObject = Instantiate(Resources.Load("Prefabs/FloorPin")) as GameObject;
         for (float x = 0; x < 15; x += 0.1f)
         {
             for (float z = 0; z < 10; z += 0.1f)
             {
                 Vector = new Vector3(xVal - x, yVal, zVal + z);
                 arrVector2 += yVal + ", ";
                 defObject2 = Instantiate(Resources.Load("Prefabs/FloorPin"), Vector, Quaternion.Euler(270, 0, 0)) as GameObject;
             }
             fArr = x * 10;
             arrVector1[(int)fArr] = arrVector2;
         }
         //Saves the generated array into a string
         string[,] strName = new string[15,10];
         strName[0,0] = arrVector1.ToString();
      }

 void save()
     {
         System.IO.File.WriteAllLines(@"E:\Test\TestFile.csv", arrVector1);
     }
 }

I have attempted to use the method outlined on this website http://wiki.unity3d.com/index.php?title=Save_and_Load_from_XML, however System.IO.File.WriteAllLines(@"E:\Test\TestFile.csv", arrVector1); doesn't accept it, giving me an error CS1502 (Best overloaded method match has some invalid arguments).

If anyone has any ideas it would be greatly appreciated.

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 zharik86 · May 12, 2014 at 07:49 AM

See this documentation from microsoft. In function WriteAllText is two argument of type string. In your case - second argument is array of string. This change to string. For example, see my code below(write on CSharp):

  public void save() {
   string tempStr = ""; //your string for file
   for(int i = 0; i < arrVector1.Length; i++) {
    if (tempStr!= "") { //add new line in our variable
     tempStr = tempStr + "\n";
    }
    tempStr = tempStr + arrVector1[i];
   }
   System.IO.File.WriteAllLines(@"E:\Test\TestFile.csv", tempStr);
  }

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 Adge · May 13, 2014 at 06:15 AM 0
Share

Thank you for your answer. I am still receiving the same "Error CS1502: The best overloaded method match for System.IO.File.WriteAllLines(string, string[])' has some invalid arguments". It says this is happening at line 114 ( System.IO.File.WriteAllLines(@"E:\Test\TestFile.csv", tempStr);` )

Is it beco$$anonymous$$g a 2D array when this:

 arrVector2 += yVal + ", ";

gets put into this?

     fArr = x * 150;
     arrVector1[(int)fArr] = arrVector2;
avatar image zharik86 · May 13, 2014 at 07:05 AM 0
Share

@Adge Initially I was guided only by your error with saving. Having looked at the Start function more attentively, it is possible to tell that your array of arrVector2 is filled only partially. Whether you need to remember coordinates of all gameObject on a scene in the text document?

avatar image Adge · May 13, 2014 at 12:21 PM 0
Share

Thanks! I have it going through now and saving them. It goes through the array again and finds the changed Y coordinates, rather than writing the changes in real-time (to save CPU power and all that). So for some reason it is limiting the array length to 94 (if you happen to have an answer to that). Your answer definitely helped with getting the save function itself working properly, though! Thanks again!

 using UnityEngine;
 using System.Collections;
 using System.IO;

 public class FloorArray : $$anonymous$$onoBehaviour{
 
     public GameObject defObject;
     GameObject defObject2;
     public GameObject frontDoorPin;
     GameObject frontDoorPin2;
 
     public float xVal = -17.14359f;
     public float yVal = -1.830479f;
     public float zVal = -1.685268f;

 
     //Variables used to save the coordinate values
     public string[] arrVector1 = new string[150]; 
     public string arrVector2;
     public Vector3 Vector;
     float fArr;
     GameObject[,] floorPinArray = new GameObject[200, 150];
 
     // Use this for initialization
     void Start () {
         //Writes the floor pins as an array
         //defObject = Instantiate(Resources.Load("Prefabs/FloorPin")) as GameObject;
         for (float x = 0; x < 15; x += 0.1f)
         {
             for (float z = 0; z < 10; z += 0.1f)
             {
                 Vector = new Vector3(xVal - x, yVal, zVal + z);
                 arrVector2 += yVal + ", ";
                 defObject2 = Instantiate(Resources.Load("Prefabs/FloorPin"), Vector, Quaternion.Euler(270, 0, 0)) as GameObject;
                 floorPinArray[(int)(x*10),(int)(z*10)] = defObject2;
             }
             //fArr = x * 10;
             //arrVector1[(int)fArr] = arrVector2;
         }
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F3))
         {
             try
             {
                 save();
                 print("YOUR COORDINATES HAVE BEEN SAVED!");
             }
             catch
             {
                 Application.Quit();
                 print("Game died :(");
             }
         }
     }
 
     public void save() {
             string[] tempStr = new string[200]; //your string for file
             for(int i = 0; i < 94; i++)
             {
                 tempStr[i] = " ";
                 for (int j = 0; j < 94; j++ )
                 {
                     tempStr[i] += floorPinArray[i, j].transform.position.y.ToString("G4") + ", ";
                 }
                    
                 //tempStr[i] = tempStr[i] + arrVector1[i];
             }
             File.WriteAllLines("E:\\Test\\TestFile.csv", tempStr);
     }
 }

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

21 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 avatar image avatar image avatar image avatar image

Related Questions

Instantiating objects at different locations 0 Answers

How do I instantiate same tag/name GameObjects with an array 1 Answer

Make object move in a direction depending on where it spawns? (C#) 1 Answer

How to only instantiate objects once? 1 Answer

Spawn from array 2 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