- Home /
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])){
"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.
I want all the buttons to have "someButton" as their text. What can I do to achieve this?
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");
}
How would you do that with a GUILayout.Button? Would you use it's GUILayout.BeginArea?
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 ();
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]);
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.
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.