Section 4, Lecture 51 - MaxGuessesAllowed - failing

Hello, I can’t get my game’s “maxGuessesAllowed” working. I’m convinced there is something wrong in my game, not something wrong with the code. I even copied and pasted the code from a the available project files to make sure it wasn’t my code that was failing.

No matter what I do, I can’t get my game to load “WinScene” after however many specified guesses. Please help.

Unity build: 4.6.9

NumberWizardScript.zip (518 Bytes)

NumberWizardUI-FullProject.zip (153.0 KB)

SOLUTION BELOW

The problem, as @Rob pointed out, was the conflict of “maxGuessesAllowed” set to 5 in my code, and “maxGuessesAllowed” set to 25 in the NumberWizard’s object’s inspector.

1 Like

Looking at the code i cant see an error that jumps out. What i would be tempted to do is a Debug log to see if the level is being called and that it is actually decreasing the maxguesses per next guess.
Add the below line to line 44 above where it loads the level. Save and play.

Debug.Log("Level Loading?");

If its calling there then the issue is with the naming of the level if not then the problem occurs either further in the code.
To check the max tries i would place the below at line 43 right below where it decreases the max guesses.

Debug.Log("%s max guesses", maxGuessesAllowed); 

(I think the last debug message is correct its been a whilse since i did C# :slight_smile: )

Hope this helps

1 Like

IrresistibleJelly,

I’m still new to c# and Unity. When you say “If it’s calling,” do you mean if it shows up in the compiler with the message “Level Loading?” If that is what you meant, it did not happen. So I tried your second line of code, and two errors appeared.

Thank you for your suggestions though, if you have anymore they are welcome.

1 Like

Okay apologies first of all it means i wrote the debug line for the max guess tries was wrong.
When i said calling i meant if it was running Application.LoadLevel and if it was you are correct in that it would show in the message log as “Leveling Loading?”

So from this we know that maxGuessesAllowed is not getting to <= 0

The actual debug log for guesses i should have wrote was.

Debug.Log(maxGuessesAllowed.ToString());

Replace what i put before with this and it should show you if it is actually lowering the number of guesses.

1 Like

You code is actually working, however you are expecting it to trigger the win scene after 5 attempts, where-as in the Inspector, if you select the NumberWizard game object in the Hierarchy, you will see you are setting the number of guesses to 25 in the Number Wizard Script component.

Either change this number, or see if you can hold out for 25 guess :wink:

1 Like

I completely missed that one lol although i think i would have got there eventually with the debugging :slight_smile:
Nice catch @Rob

1 Like

I couldn’t see anything wrong with the code initially, so I grabbed the project and ran it. Sure enough after counting to 5 nothing was happening. I looked all through it and still didn’t spot anything.

It wasn’t until I added a;

Debug.Log("maxGuessesAllowed at Start(): " + maxGuessesAllowed);

…I then got a response of 25!

Of course, before I looked for the obvious, I dived in deep and tried to work out how it could be taking the value of five and iterating through 5 times and adding it together…

/facepalm

Then I spotted the 25 being set in the Inspector… chuckle… had to laugh at myself at that point.

One thing I would add regarding the use of Debug.Log(), I always tend to add a bit of a message in the statement regardless of the value that I am trying to return, just in case what I am trying to return doesn’t actually return anything, the console doesn’t show anything if there wasn’t any output at all, whilst most types will have a default value, strings can be a pain :slight_smile:

1 Like

Thank you so much @Rob, I went to the number wizard object, and sure enough the problem was in the inspector. I reset it to 5 and works great now. Thanks a lot @Marc_Carlyon too!

2 Likes

More than welcome, and there’s some great advice there from @Marc_Carlyon regarding the use of the Debug.Log() statements, they come in really useful when these little things happen, don’t be afraid to pop some in :slight_smile:

1 Like

Completely agree, I’ve worked with both Unity and Unreal courses and the same thing is consistant with both and its Debug.Log.
Glad i could be of some help :slight_smile:

Hi I had the same problem. I changed the code to 5 guesses for the computer, but left the 10 in the script of the NumberWizard in the Inspector. Perhaps that’s something to be said in lesson 54. I see that there are nice grafic games posted, but many still have 10 guesses for the computer.

I had the same issue as well, but could debug the same way that explained here before coming to the forum :slight_smile: Took me a while but it’s satisfying to find the solution ourselves.

Privacy & Terms