How would you write a forloop for letters rather than numbers?

I used choice A,B, and C for my game and when I tried to do the forloop, it does not work. I am assuming it’s the wording for the forloop code needing to reflect letter keycodes rather than number keycodes?

Hi Pigor,

I’m afraid I do not understand what you mean. The GetKeyDown method takes in a KeyCode value. A itself is not a type C# can recognise. KeyCode.A is.

Theoretically, you should be able to make Rick’s solution work with A, B, C, and so on.

Click on KeyCode in Visual Studio, then press F12. Scroll down to A in the list. If the B has got the value of A + 1, you can do the same as with KeyCode.Alpha1.

Thank you for the response.

I do have the letters A, B and C entered as KeyCode.A, KeyCode.B and KeyCode.C within the code but the game only works in the Unity program (it throws up some warnings, but nothing to do with the KeyCodes.) My code worked and I never got the errors that were discussed in forloops, but when built it I would get a blank screen if I made a choice not offered.

I used Ricks solution, and when I built the game it now only gives you state A for every screen. The text on screen still looks normal, of course…but if you choose option A, B or C you will always get the nextstate that corresponds with A.

When I reverted it back to the original code (without the solution) the game plays beautifully in Unity but when built it will give you a screen with no text if you chose any option not available (if you choose C when there is only choice A & B).

Sorry it’s confusing, but I am trying to learn. I’ve never dealt with code before.

Also, as a side note, I could never get the errors discussed in this Text101 class. I got some warnings but no errors. I sent a copy of the game to my husband and his brother and both of them saw errors…they just don’t show on my screen.

Here are snips of my game code and states code:

lemminggamecodesnipstates

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Your code looks fine apart from the superfluous curly brackets in lines 35 and 53. It should work.

When playing your build open the browser console. Is there a NullReferenceException? If so, this could be the problem. NullReferenceException means that a reference (“link”) to an instance is missing. If you exposed a field in the Inspector, make sure that it’s not empty.

Hope this helps :slight_smile:


See also;

Thank for the reply. I will try to make things easier in the future.

I tried using the substitution Rick showed again and got the same results, but then my brother in law saw I had not entered state = nextStates[index]; I had state = nextStates[0]; so it kept looping to choice A…

Basically it was just a code mistake I couldn’t see myself for whatever reason…blind, I guess.
Thanks again for the response :slight_smile:

Oh, your screenshot shows something different. I’m glad your browser in law spotted the issue. As the saying goes, two heads are better than one. :slight_smile:


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms