storing info in arrays

hi guys, after failing to use streamwriter i’ve attempted to use playerprefs instead. what im trying to do is set usernames in arrays rather simply on a new line of text with streamwriter. regardless could anyone tell me if the concept of this would actually work? becuase im having trouble assasinging new names.

var Name = "input name";
var NameInp = "";
var Users = new Array ();

var FirstNameUsed = false;


var V1 : Vector2 = Vector2 (100,100);
var V2 = 200;
var V3 = 20;

var HoldName = "";
var Name2 = "";


function Update () {
if(Name != "input name"  Input.GetKeyDown("return")  FirstNameUsed == false) {
	PlayerPrefs.SetString("Name", Name);
	Users.Push(Name);
	print(Name);
	FirstNameUsed = true;
	HoldName = Name;
	}
if(FirstNameUsed  HoldName != Name  Input.GetKeyDown("return")) {
	Users.Push(Name);
	Users[1] = Name;
}
if(Input.GetKeyDown("r")) {
	print(Users[0]);
	}
if(Input.GetKeyDown("l")) {
	print(Users[1]);
	}
NameInp = Name;
}
function OnGUI () {
Name = GUI.TextField(Rect(V1.x,V1.y,V2,V3), Name, 20);
	}

say i wanted to create a game where its used strictly on one system, or database. I want it to create a new name everytime rather than keeping, or overwriting the same name. Is there any more efficient way to do this?

EDIT: I’ve relised that you don’t even need to do half the crap i did! the push function just adds a new name regardless lol FML :stuck_out_tongue:

ok now, i want to store each array element to a playerprefs value, any ideas on how to do this other than creating like 10000 vars for example

var 1 = Users[1];
var 2 = Users[2];
var 3 = Users[3];

Use ArrayPrefs.

–Eric

hey eric thanks for the reply, but im having a little trouble understanding how its works :smile: heres an example of my code could you help me rectify? thanks

var Name = "input name";
var NameInp = "";
var Users = new Array ();

var FirstNameUsed = false;


var V1 : Vector2 = Vector2 (100,100);
var V2 = 200;
var V3 = 20;

var HoldName = "";
var Name2 = "";




function Update () {
if(Name != "input name"  Input.GetKeyDown("return")  FirstNameUsed == false) {
	PlayerPrefs.SetString("Name", Name);
	Users.Push(Name);
	print(Name);
	FirstNameUsed = true;
	HoldName = Name;
	var Name1 = Users[0];
	var names = new String[10];
	for (i = 0; i < names.Length; i++)
    names[i] = "Player"+(i+1);
	if (!PlayerPrefsX.SetStringArray("Users", Users))
    print("Can't save names");
	PlayerPrefsX.SetStringArray("Users", Users);

	}
if(FirstNameUsed  HoldName != Name  Input.GetKeyDown("return")) {
	Users.Push(Name);
	
}
if(Input.GetKeyDown("r")) {
	print(Users);
	}
NameInp = Name;
}
function OnGUI () {
Name = GUI.TextField(Rect(V1.x,V1.y,V2,V3), Name, 20);
	}

It has to be a string array (i.e., “var myStrings = new String[20];”), not a generic Array.

–Eric

but would this be compatable with my script? as i cant use Push with string? is there any other way to add stuff to an array in a similar way?

You can convert an Array into a string array with ToBuiltin.

–Eric

im not sure how to put/use .net functions into unity :\ this is why i couldnt get streamwriter to work :frowning:

the same way you do it in any other environment too.

you import the corresponding namespace (using System.IO for streamwriter for example if I recall correctly) etc.

I fear that C# basics and should be learnt before dealing with Unity or more advanced things.

the UnityScript counterpart is @import

ToBuiltin isn’t a .net function; see the docs for the Unity Javascript Array class.

–Eric