Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Refar8 · Feb 16, 2019 at 09:35 AM · arraystringreturnreturn value

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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;
      }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Refar8 · Feb 17, 2019 at 05:54 PM 0
Share

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

avatar image dan_wipf Refar8 · Feb 17, 2019 at 08:42 PM 0
Share

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]);
 }

  1. 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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges