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 karenkrane · Mar 15, 2013 at 05:36 PM · c#guiimagestoolbar

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.
       }
     }
 }
Comment
Add comment · Show 2
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 karenkrane · Apr 17, 2013 at 03:45 PM 0
Share

Can nobody help with this?

avatar image Yokimato · Apr 17, 2013 at 03:48 PM 0
Share

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.

1 Reply

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

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.

Comment
Add comment · Show 4 · 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 Yokimato · Apr 17, 2013 at 03:50 PM 0
Share

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)

avatar image whydoidoit · Apr 17, 2013 at 03:51 PM 0
Share

I do and it does.

avatar image whydoidoit · Apr 17, 2013 at 05:51 PM 1
Share

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.
       }
     }
 }
avatar image karenkrane1 · Apr 19, 2013 at 05:33 PM 0
Share

Thank you!

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

13 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

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


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