- Home /
Return array of strings
Hello all,
I have a code like this that cant work. What is the problem?
public string[] getHealthPickupTag()
{
for(int i = 0; i < myPUM.healthPickups.Length; i++)
{
return myPUM.healthPickups[i].gameObject.tag;
}
}
I am saying to it that I have a array but why it says it can return only one value? Thanks for any help.
Answer by dan_wipf · Feb 16, 2019 at 09:42 AM
well you need to return an array of string, right now i assume you return a single string(tag) which isn’t a valid return. you might want to try this:
public string[] getHealthPickupTag()
{
string[] s = new string[myPUM.healthPickups.Length];
for(int i = 0; i < myPUM.healthPickups.Length; i++)
{
s[i] = myPUM.healthPickups[i].gameObject.tag;
}
return s;
}
thanks @dan_wipf it works but I added a line of debug.log(getHealthPickUpTag()) but it prints only (object). why? means it does not show me the array that stored in s. and how can I store these array produced from that s in new array to use them later? Basically I am trying to get tags of some pick ups which might be different number in each scene then store them in another array to use later. (if I want to be clear I need to do this for serialization for save system)
thanks for help
ok 1. i gonna move your answer to comment 2. please accept answer as right for others 3. well you just reference the string object not what’s inside what you could do is a for loop and debug it so like:
string[] myhealth = getHealthPickupTag();
for (int i = 0; i<myhealth.Length;i++){
Debug.Log(myhealth[i]);
}
you can declare a property if it’s a non dynamicly array (changes over time) after your class and later you’d call something like:
public string[] myarray;
void Start() {
myarray = myarraysetup();
}
or if tags are created you might want to do this in the Update part
Your answer
Follow this Question
Related Questions
Split String into Arrays 1 Answer
how can i split a word to individual letters..?? 2 Answers
Array Slit String and Send NGUI text 0 Answers
Newbie Problem with Multidimensional Arrays in JavaScript 0 Answers
Making a GUI Label carrying a variable update when changed to all clients on network 0 Answers