Can't change number of guesses allowed

Hi all,
My Number Wizard seems to be running fine except I can’t seem to change the number of guesses the Wizard has before triggering the “Win” scene.
I have changed them so the Wizard should only have 2 guesses but it still gets 10 every time.

I have tried changing the number of guesses in the script in MonoDevellop and in the Inspector in Unity, both independently and simultaneously.

I have also tried removing the script component from “Guess” in the Inspector.
Neither seem to have any effect.

Here is my Code:

Here is the Scene open in Unity:

1 Like

Hey @first_fuerst,

I am actually having the same problem with my Number Wizard UI and I saw that you solved this one. Would you be able to let me know how you solved this one as mine also keeps the cap at 10 tries no matter what. Thanks!

1 Like

Hey @akatori,

had the same issue and just fixed it by removing the public from the maxGuesses variable :slight_smile:

Hope it’ll work for you !

1 Like

Hi guys, I was having the same problem. It isn’t really a problem, but a side effect of making the variable public. Notice when in Unity after making the variable public, it appears in the inspector when you select the NumberWizard object in the hierarchy.

I don’t know why, but after that point, changing the maxGuessesAllowed in the code doesn’t change the variable in the inspector, but you can change the variable value in the inspector directly. Hope this helps.

2 Likes

Hi Andy,

I don’t know why, but after that point, changing the maxGuessesAllowed in the code doesn’t change the variable in the inspector

The Inspector value overrides the values which are set as member variables. You can, however, move the initialisation of the variable to either the Awake or Start methods, leaving the member variable declaration at the top of the class, for example;

using UnityEngine;

public class Example: MonoBehaviour {

    public string playerName;    // declare

    void Awake () 
    {
        playerName = "Andy";    // initialise
    }
	
    void Update () 
    {
        Debug.Log(playerName);
	}
}

In the above example, if you were to attach this to a GameObject and run the game, the string entered in the Player Name field within the Inspector would change to “Andy”, then, when the game stops, it would revert back to what was in the Inspector.

In the above replies, because public is changed to private the field is no longer available to the Inspector and as such there is no value to use.

Hope this is of use :slight_smile:

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