- Home /
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();
}
}
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
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.
Never$$anonymous$$d, I simply had a mistake lower in my code that I hadn't noticed.
string [] itemName;
void Awake ()
{
itemName = new string [ differentItems ];
}
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 ;)
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();
}
}