- Home /
Saving Vector2 coordinates as waypoints to dynamic array
Hi there,
I am wanting to write a script that allows me to control my player car and every half second or so, store the X/Y coordinates to an array, increasing the size of the array as it does so. Then I can use the data stored in the array as waypoints for my AI cars to drive between (can also use it as ghost data).
I understand how to set up arrays and add the data to them, the question I have is... how do I make it so that I can access that data inside the array and store it for use with another car without being zeroed every time I run the scene?
Would I need to save the data to an external file and then read it back in? I'm a bit of a Unity/game dev noob, so this stuff seems a little tricky to me but once I have the "idea" of how to do it, I should be able to work it out. I'm not asking for scripts or code samples.
The only way I could make it work right now would be to print the values to the debug console and manually go through them and add them to the AI script by hand... that seems silly. LOL.
Any help would be greatly appreciated ;-)
Hmm... that was long-winded... in a nutshell... how do I add values to an array and keep them without being zeroed on next run? Cheers :)
Answer by Berenger · Feb 04, 2012 at 11:17 PM
You can use playerprefs to do that. Unfortunately, there isn't a Get/SetArray, so you can use this instead. (From Eric5h5)
Thanks! Can I just set the array in one script to equal the array from another script, as you can with simple variables?
Oh, I thought you had to change the entire scene ! Well yes, you can totally do (Edit)
int[] array1= { 0, 1, 2 };
int[] array2= new int[ array1.Length ];
System.Array.Copy(array1, array2, array1.Length);
Ah, no, sorry... not changing scenes! Cool, that makes it simpler. I'll just need to work out playerprefs now. LOL.
Your answer
![](https://koobas.hobune.stream/wayback/20220613063738im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Cannot save(serialize) a double array of a custom class 1 Answer
Why is everything being set the same? Please Help! 1 Answer
Follow up to AI Pathfinding Question 0 Answers
how to instantiate gameobjects in a 2d array. 2 Answers
How how can I make AI only move towards objects that are active in the scene 0 Answers