#like this
1 messages · Page 1 of 1 (latest)
Unity's KeyCode is an enum, which means that under the hood it's just a fancy way to give names to a bunch of numbers.
If you know which numbers are associated with the names you're interested in (alphabet keys for you, I assume?), you can generate a random number that fits them, and then cast (transform) it into the enum to get a value you can use - That's what Dalphat's answer is showing you.
This cool gist has a bunch of values for Unity's keycodes, I hope that helps you figure it out 🙂 https://gist.github.com/Extremelyd1/4bcd495e21453ed9e1dffa27f6ba5f69
Just a heads-up for next time, in #854851968446365696 it mentions "Do not share screenshots of code unless requested."
Ah sorry
It looks like you're not casting your number to a KeyCode. Casting between types looks like this: (int)2.5f, which casts a float into an int
Im struggling on how to get the values out
No worries for my behalf, I'm just letting you know 🙂
So would it be if (Input.GetKey(KeyCode(110)))
Your current code looks like you're giving 110 as a parameter / argument to KeyCode - like KeyCode(110).
Instead, you should cast the int to your enum - that would look like: (KeyCode)110
I know that they're pretty similar, so it's not easy to spot at a glance 😅
Sorry only sending a screen shot because it flags as an error
The error message looks like it's confused because it doesn't know what KeyCode "means".
Do you have a variable or method defined in your script that is also called KeyCode?
No just the standard original KeyCode from unity
Hm. Is BattleManager something from your project?
thats the class im workingg on
Are you sure there's nothing in that script / file called KeyCode, except from the part in Input.GetKey?
Ok, so yeah, you've defined a new KeyCode enum. You don't need to do that, Unity already has one
The error is because Unity doesn't know which KeyCode (its own, or your new one) you mean when you write (KeyCode)110 - but that looks right otherwise.
Ok im getting confused haha, So this
{
A = 97, // 0x00000061
B = 98, // 0x00000062
C = 99, // 0x00000063
D = 100, // 0x00000064
E = 101, // 0x00000065
F = 102, // 0x00000066
G = 103, // 0x00000067
H = 104, // 0x00000068
I = 105, // 0x00000069
J = 106, // 0x0000006A
K = 107, // 0x0000006B
L = 108, // 0x0000006C
M = 109, // 0x0000006D
N = 110, // 0x0000006E
O = 111, // 0x0000006F
P = 112, // 0x00000070
Q = 113, // 0x00000071
R = 114, // 0x00000072
S = 115, // 0x00000073
T = 116, // 0x00000074
U = 117, // 0x00000075
V = 118, // 0x00000076
W = 119, // 0x00000077
X = 120, // 0x00000078
Y = 121, // 0x00000079
Z = 122, // 0x0000007A
}```
is wrong and i dont need to use that? or i dont need to use the name of the enum
You don't need that at all.
Sorry, I wasn't clear.
When I sent you the link, I didn't mean 'use this directly' - I meant someone has already written down which Unity KeyCode is which number, so you had some help figuring out which numbers to generate.
For example, if you wanted to generate a number key (known as Alpha- something in KeyCode), you would generate a number between 48 and 57
Then, you can cast that number into a KeyCode to use with your Input check
Haha i fully understand now, many thanks jowr
Sorry one more question can i reverse that
blockLetter.text = ;
can i set the text to be the random int value but in string form i.e if 110 was casted could i convert that to Get the letter N in return so then the text on the screen would say "N"
I think that enums support .ToString() - if not, there is Enum.GetName() in the framework - https://learn.microsoft.com/en-us/dotnet/api/system.enum.getname?view=net-7.0
Thanks awsome thanks