- Home /
converted array item to string returns "s,tri,ng" with commas?
Im trying to make an inventory. Everything works perfectly except for one thing, when scrolling between items i noticed all "strings" listed are seperated like this: (m,e,yo,u,h,er,hi,m). I dont want commas at all.. can anyone see why this random comma appears to be seperating my strings? i use Unityscript / Javascript.
var itembagCursor : int = 0;
var itembagCursorMax : int = 0;
var selectedItem;
var selecteditemname;
function Start(){
Master.itembag.AddRange(Array("potion","shard","antidote")); //add item to inventory
itembagCursorMax = itembagCursorMax + 2;
Master.inventory.AddRange(Array("key","key","ammo","ammo")); //add multiple items
Master.inventory.Sort(); //sort inventory
Master.inventory.Remove("ammo"); //remove first instance of item
}
function Update(){
selectedItem = Array(Master.itembag[itembagCursor]).ToString();
if(Input.GetKeyDown(KeyCode.LeftArrow)){
itembagCursor = itembagCursor - 1;
}
if(Input.GetKeyDown(KeyCode.RightArrow)){
itembagCursor = itembagCursor + 1;
}
if(itembagCursor > itembagCursorMax){
itembagCursor = 0;
}
if(itembagCursor < 0){
itembagCursor = itembagCursorMax;
}
}
function OnGUI(){
GUI.Label(Rect(0,0,400,50), "Inventory: " + Array(Master.inventory)); //display inventory by converting it to an array
GUI.Label(Rect(Screen.width/2 - 200,Screen.height - 50,400,50),selectedItem);
}
Answer by Bunny83 · Jan 21, 2013 at 12:15 PM
This makes no sense:
selectedItem = Array(Master.itembag[itembagCursor]).ToString();
Your items are strings, you create a new array from your selected string which will split the string into characters.
selectedItem = Master.itembag[itembagCursor];
btw you use a lot untyped variables as well as the slow Array class. You should specify a type for each variable and better use a generic List. Can't go more in detail at the moment.
ok, thank you for your reply, so what are you saying in short? i need to remove the "ToString();" in the first line you pasted because its converting a string to a string?
never$$anonymous$$d my last comment. i figured out what you ment. i feel silly. thank you very much @Bunny83.