- Home /
GUI Texture in toolbar (C#)
Hi I'm trying to set up a toolbar with images as buttons. But I'm struggling, I think with the array statement: I get error CS0236: A field initializer cannot reference the nonstatic field, method or property 'myGUI.buttonNumber' for each button.
Here's my code:
using UnityEngine;
using System.Collections;
public class myGUI: MonoBehaviour {
public Texture2D button1;
public Texture2D button2;
public int myToolbar = 0;
public Texture[] toolbarIcons = new Texture[] {button1, button2};
void OnGUI(){
myToolbar = GUI.Toolbar (new Rect (100,100,100,100), myToolbar, toolbarIcons);
if (GUI.changed) {
//etc.
}
}
}
nothing is jumping out to me with that code...but it looks like you omitted some. Can you replace what you have with all of it to get the full picture. The error message usually indicates that you're attempting to access a non-static reference from a static function...but I'm not seeing that with what you have.
Answer by whydoidoit · Apr 17, 2013 at 03:46 PM
Do as it says and don't initialize the array of toolbarIcons in an initializer - set them in Awake instead.
hmm... I'm not sure that would cause a compile time error. (Don't have access to a comp with unity to test it though)
To be clear:
using UnityEngine;
using System.Collections;
public class myGUI: $$anonymous$$onoBehaviour {
public Texture2D button1;
public Texture2D button2;
public int myToolbar = 0;
public Texture[] toolbarIcons;
void Awake()
{
toolbarIcons = new Texture[] {button1, button2};
}
void OnGUI(){
myToolbar = GUI.Toolbar (new Rect (100,100,100,100), myToolbar, toolbarIcons);
if (GUI.changed) {
//etc.
}
}
}
Your answer
Follow this Question
Related Questions
Switch Between GUI images 0 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Changing GUI.Box opacity 3 Answers
Store/Stack Items 0 Answers