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
1
Question by Klasmic · Jul 14, 2017 at 09:49 AM · arraystringcs0119

String array not working

ive been watching a tutorial on making inventory systems in unity but I cant figure out why this one bit keeps on giving me errors in the tutorial (https://youtu.be/3RW3FgIFYSU?t=166) he tells us to create a string array in his version in the [] brackets he has no number but mine gives me errors because of this and when I put the right number in (9) it gives me the error Expression denotes a value', where a `method group' was expected

I cant see what he did clearly in the video because of the crappy quality, and the source code is not updated to this episode

heres my code string[] names = new string[9]("Grass", "Dirt", "Stone", "Dry Sand", "Wet Sand", "Coal", "Iron", "Gold", "Diamond" );

Comment
Add comment · Show 1
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 Bunny83 · Jul 14, 2017 at 10:29 AM 0
Share

When you actually set the video quality to 1080p and go full screen it can be seen pretty clear. Here's a screenshot:

1 Reply

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

Answer by SohailBukhari · Jul 14, 2017 at 10:00 AM

basically this repenting the array size you don't need to initialize

 string[] names= { "Grass", "Dirt", "Stone", "Dry Sand", "Wet Sand", "Coal", "Iron", "Gold", "Diamond"};

Edit: Thanks to @Bunny83

The problem is that you used normal brackets "( )" instead of curly brackets " { }" for the initialization block.

For classes, for example String, it's the same:

  • String[] myStringArray = new String[3];

  • String[] myStringArray = {"a","b","c"};

  • String[] myStringArray = new String[]{"a","b","c"};

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 Bunny83 · Jul 14, 2017 at 10:15 AM 1
Share

You are right with your example but the array size is not the problem. The problem is that he used normal brackets "( )" ins$$anonymous$$d of curly brackets " { }" for the initialization block.

This does work just fine:

 string[] myStringArray = new string[3]{"a","b","c"};

However you have to make sure you supply the same number of initialization values as the array size specifies. So new string[4]{"a","b","c"}; or new string[2]{"a","b","c"}; would cause a compilation error. In most cases you would omit the array size because it's easier to extend the array because you don't have to take care of the size as well. However in some cases it might be useful when the array must have a certain size so a programmer (doesn't have to be you) doesn't accidentally add or remove elements.

ps: $$anonymous$$eep in $$anonymous$$d that the short initialization method:

 string[] myStringArray = {"a","b","c"};

does only work when used directly in a field initializer / variable initialization. In all other cases this does not work

 string[] array;
 array = {"a","b","c"};  // <<-- error
 array = new string[]{"a","b","c"}; // <<-- correct
 
 void Test(string[] arr)
 {
 }

 Test({"a","b","c"});  // <<-- error
 Test(new string[]{"a","b","c"}); // <<-- correct
avatar image SohailBukhari · Jul 14, 2017 at 11:04 AM 0
Share

You are right the main problem was brackets.

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

78 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

Related Questions

Access all Items in an Array 4 Answers

how to randomly pick a string from an array 3 Answers

Convert Array into One String (Js) 1 Answer

how to get game object by string? 0 Answers

Extending the Array class? 1 Answer


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