Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Apples_mmmmmmmm · Dec 23, 2011 at 09:44 PM · guiarraysconstant

Assets/Scripts/Inventory.cs(9,40): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `Inventory.differentItems'

I'm essentially trying to create a simple inventory type list using unity's GUI. However I receive this error when trying to run my script. I believe the problem is caused by the variable different items not being constant. If anyone could show me how I should call differentItems as a constant (if that truly is where the error is occurring) that would be extremely helpful.

 using UnityEngine;
 using System.Collections;
 
 public class Inventory : MonoBehaviour 
 {
     int differentItems = 200;
     int[] itemCount = new int[200];
     int fifthWidth = Screen.width/5;
     string[] itemName = new string[differentItems];
     public Vector2 scrollPosition = Vector2.zero;
     void OnGUI () 
     {
         scrollPosition = GUI.BeginScrollView(new Rect(Screen.width - fifthWidth, 0, fifthWidth, Screen.height), scrollPosition, new Rect(Screen.width - fifthWidth, 0, fifthWidth, (Screen.height/20) * differentItems));
         for(int i = 0; i < differentItems; i++)
         {
             GUI.Button(new rect(0,(Screen.height/20 * i), fifthWidth * (4/5), Screen.height/20), itemName[i]);
             GUI.Label(new rect(fifthWidth * (4/5),(Screen.height/20 * i), fifthWidth * (1/5), Screen.height/20), itemCount[i]);
         }
         GUI.EndScrollView();
     }
 }
Comment
Add comment · Show 6
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 jahroy · Dec 23, 2011 at 10:02 PM 2
Share

I know this isn't what you asked, but you can use the code you have if you do it in the Awake function.

A quick google suggests you can make a variable a constant with the const keyword:

http://msdn.microsoft.com/en-us/library/e6w8fe1b(v=vs.80).aspx

avatar image Apples_mmmmmmmm · Dec 23, 2011 at 10:42 PM 0
Share

Thanks for the reply. I tried using the const keyword, but it only seemed to make things worse. Perhaps I wasn't using it in the correct context. Would you $$anonymous$$d posting the code with the Awake function? I'm unsure what you code you incorporated in the function.

avatar image Apples_mmmmmmmm · Dec 23, 2011 at 10:47 PM 0
Share

Never$$anonymous$$d, I simply had a mistake lower in my code that I hadn't noticed.

avatar image jahroy · Dec 23, 2011 at 10:49 PM 2
Share
 string [] itemName;

 void Awake ()
 {
     itemName = new string [ differentItems ];
 }
avatar image by0log1c · Dec 23, 2011 at 11:43 PM 1
Share
  • to Jahroy comment of using Awake. $$anonymous$$aking your variable a const is merely a workaround, possibly modifying underlying behaviour. Use the Awake function as the runtime constructor like you are supposed to for correct behaviour. $$anonymous$$arking variable static or const is not something to be done lightly. Hardcoding the value isn't the best option neither so let's just stick with best practise and use Awake ;)

Show more comments

1 Reply

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

Answer by Apples_mmmmmmmm · Dec 23, 2011 at 10:49 PM

 using UnityEngine;
 using System.Collections;
 
 public class Inventory : MonoBehaviour 
 {
     const int differentItems = 200;
     int[] itemCount = new int[200];
     int fifthWidth = Screen.width/5;
     string[] itemName = new string[differentItems];
     Vector2 scrollPosition = Vector2.zero;    

 void OnGUI() 
 {
     scrollPosition = GUI.BeginScrollView(new Rect(Screen.width - fifthWidth, 0, fifthWidth, Screen.height), scrollPosition, new Rect(Screen.width - fifthWidth, 0, fifthWidth, (Screen.height/20) * differentItems));
     for(int i = 0; i < differentItems; i++)
     {
         GUI.Button(new Rect(0,(Screen.height/20 * i), fifthWidth * (4/5), Screen.height/20), itemName[i]);
         GUI.Label(new Rect(fifthWidth * (4/5),(Screen.height/20 * i), fifthWidth * (1/5), Screen.height/20), itemCount[i].ToString());
     }
     GUI.EndScrollView();
 }

}

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

How to select a texture in an inventory? 0 Answers

text fields and GUI 1 Answer

Customize arrays and classes in inspector window 2 Answers

How can I make an array fill the contents of my GUI List? 1 Answer

Making A Question Based Game, How to Ask Randomly 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