room list as an array in unity

0 votes

i am tryiing to get the room ids of the rooms that i created on the console to display as gui in my game. by default the listener script on calling the function ongetallroomsdone() displays the room ids as log using the code:-

Log ("onGetAllRoomsDone : " + eventObj.getResult());
for(int i=0; i< eventObj.getRoomIds().Length; ++i)
{
Log ("Room ID : " + eventObj.getRoomIds()[i]);
}
in order to display the list as GUI in my game i decided to create a string array k and assign the room ids to it in the loop and then display it.but when i make the following changes it stops after displaying the first room id in the log and the debug.log i put .please help!
 
public String[] k;
public void Start()
{
k=new string[20];
}
public void onGetAllRoomsDone (AllRoomsEvent eventObj)
{
 
Log ("onGetAllRoomsDone : " + eventObj.getResult());
for(int i=0; i< eventObj.getRoomIds().Length; ++i)
{
Log ("Room ID : " + eventObj.getRoomIds()[i]);
k[i-1]=eventObj.getRoomIds()[i];
}
 
}

 

asked Jul 10, 2015 in AppWarp by Mihir Solanki (27 points)

1 Answer

0 votes

k[i-1]=eventObj.getRoomIds()[i];

when you try to access item with index "i-1" you may try to access [-1] with i=0 . thus it should throw an error right there. Check whether you really want to access to the array member with index of "-1". therefore using ++i should not stop this from happening. My solution suggestions are:

1-) Check if an item exists prior to accessing it.

2-) Decide if you really need to access (i-1) th index in your for loop, if not change it so other elements get decent indexes such as :

for(int i=0; i< eventObj.getRoomIds().Length; i++)
{
Log ("Room ID : " + eventObj.getRoomIds()[i]);
k[i]=eventObj.getRoomIds()[i];
}
answered Jul 10, 2015 by onurdilek (10 points)
k got it onurdilek
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...