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 Ochreous · Aug 27, 2013 at 05:04 AM · c#texturechar

C# cannot convert 'char' expression to type 'UnityEngine.Texture'

Hi everyone, I'm trying to use a for loop with one of my GUILayout.Buttons . But for some reason I'm getting this error cannot convert char' expression to type UnityEngine.Texture'. I don't know why it believes I'm trying to use a texture.

                for (int i = 0; i < 5; i++)
                if (GUILayout.Button("someButton"[i])){
Comment
Add comment · Show 4
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 TheRaven · Aug 27, 2013 at 05:09 AM 0
Share

"somebutton" is a string;

It needs to be an object. Looks like you have an array of buttons.

 srting[] buttons = new srting[]{ "oneButton", "twoButton","threeButton", "fourButton", "fiveButton" };
             
     for (int i = 0; i < 5; i++)
     {
       if (GUILayout.Button(buttons[i]))
       {
                     
       }
     }

something like that.

I think it thinks its a texture because Texture is the first param of one of the overloaded function declarations.

Basically the compiler doesnt actually know what its being fed because its unclear atm.

avatar image Ochreous · Aug 27, 2013 at 05:26 AM 0
Share

I want all the buttons to have "someButton" as their text. What can I do to achieve this?

avatar image TheRaven · Aug 27, 2013 at 05:33 AM 0
Share

If you want to create 5 buttons with text somebutton

     for (int i = 0; i < 5; i++)
     {
         GUI.Button(new Rect(0, 10*i, 100, 100), "somebutton");
     }
avatar image Ochreous · Aug 27, 2013 at 05:36 AM 0
Share

How would you do that with a GUILayout.Button? Would you use it's GUILayout.BeginArea?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by TheRaven · Aug 27, 2013 at 05:39 AM

yes

     GUILayout.BeginArea (new Rect (10,10,100,100));
     for (int i = 0; i < 5; i++)
     {
         GUILayout.Button("somebutton");
     }
     GUILayout.EndArea ();
Comment
Add comment · Show 3 · 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 Ochreous · Aug 27, 2013 at 06:02 AM 0
Share

I don't understand, What do you do with the GUILayout.BeginArea? I also need to implement another integer with the GUILayout.Button. How exactly do I do that? For a while I was using the following below but since this is a single string how would that work?

 GUILayout.Label(someArray[i + someInt]);
avatar image TheRaven · Aug 27, 2013 at 06:56 AM 0
Share

The for loop is only there to make 4 buttons in the above, notice that 'i' is never uses inside the loop. GUILayout does auto-layout and the begin area allows you to specify the constraints of the layout.

avatar image graslany · Aug 27, 2013 at 07:14 AM 1
Share

I'm not sure what you mean exactly by "implementing an integer in a button". If you want each of your buttons to display the text "somebutton <#>" where <#> would be 1 on the first button, then 2 on the second, etc. then you can modify TheRaven's snippet like this :

 GUILayout.BeginArea (new Rect (10,10,100,100));
 for (int i = 0; i < 5; i++)
 {
 GUILayout.Button("somebutton " + (i+1));
 }
 GUILayout.EndArea ();

What this snippet does is to execute 5 times the instruction in the body of the for loop, and this insctruction creates a button. As you can see here, there are several ways of creating a button by supplying to GUILayout.Button() either a string, a texture, etc ... (this is what TheRaven and software developpers in general refer to as overloading).

What I do here is using the version of GUILayout.Button() that takes a string as parameter. And we build this string using the following expression : "somebutton " + (i+1). When you add a string with an integer in your code, you express that your program must convert the integer into a string of characters representing this number, and then concatenate that string to the first string.

Putting it all together, this program will make 5 of these (conversion + concatenation) operations with a different value of i each time due to the for loop, and use each resulting string ("somebutton 1", "somebutton 2", etc ...) to create GUI Buttons.

Edit : Regarding your original post, the expression "someButton"[i] does not perform the (conversion + concatenation) operation I was talking about above ; what it does is referring to the i-th character of the string, starting at index zero. Therefore "someButton"[0] for example is strictly equivalent to the character 's', "someButton"[1] is 'o', "someButton"[2] is 'm' and so on. This is because in most (if not all) program$$anonymous$$g languages a string is the same as an array of characters. You may want to read some good book on computer program$$anonymous$$g (and in particular arrays) if you are not at ease with these basics.

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

17 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

Related Questions

null texture passed to GUI.DrawTexture 0 Answers

C# combining Two Textures 3 Answers

How to load image from redirected server URL? 1 Answer

how to make high quality textures with low size of downlaod 1 Answer

How to Name Individual Buttons Within a List 2 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