Question Unity and C# Navmesh and Boolean help

Dj6974

New member
Joined
Aug 28, 2022
Messages
3
Programming Experience
Beginner
I'm making a 3D game in Unity and I'm using the NavMesh to move the main character around the game. The code to move the player works fine unless I try to add a boolean to the mix. I want to make it so the player only moves when a certain bool is true. But when I do this, the player doesn't move at all whether the bool is true or not.
C#:
void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, 100))
        {
            agent.SetDestination(hit.point);
        }
    }
}
Works fine.
C#:
void Update()
{
    if (Input.GetMouseButtonDown(0) && somebool == true)
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      
        if (Physics.Raycast(ray, out hit, 100))
        {
            agent.SetDestination(hit.point);
        }
    }
}
Doesn't work at all.
 
Last edited:
Welcome to the forum. In the future please put your code in code tags when posting code.

Are you sure? Right now the code that posted doesn't look like it will even compile. Neither line 1 of both chunks look like valid method signatures.

Anyway, assuming that you just mistyped stuff from memory for the method signatures, try the following as a sanity check: between lines 2 and 3 of the second chunk insert the following line:
C#:
somebool = true;

If that works, then that means that your statement that the code doesn't work regardless of somebool being true or false is not correct. It only doesn't work when somebool is false, which is exactly what line 3 is trying to filter out.
 
Last edited:
Editing Post #2....
 
Welcome to the forum. In the future please put your code in code tags when posting code.

Are you sure? Right now the code that posted doesn't look like it will even compile. Neither line 1 of both chunks look like valid method signatures.

Anyway, assuming that you just mistyped stuff from memory for the method signatures, try the following as a sanity check: between lines 2 and 3 of the second chunk insert the following line:
C#:
somebool = true;

If that works, then that means that your statement that the code doesn't work regardless of somebool being true or false is not correct. It only doesn't work when somebool is false, which is exactly what line 3 is trying to filter out.

Thanks for the clean up and yeah I made some copy/paste mistakes.

So I originally ran this while setting somebool to true when I declared it but it didn't work. Then I set somebool to true in the Start method and it started working. However it doesn't acknowledge when I change somebool to false or vice versa while the game is running. The game data shows that somebool is being turned on or off but the game only functions according to the initial value set in the Start method.
 
Set a breakpoint in your Update() method. Inspect the value of somebool at the time the breakpoint is hit. Is it the expected value that matches your game data?

More often than not, beginners run across the issue that they don't keep track of the scope of their variables. They expect a variable named the same but in a different scope to automatically update another variable with the same name in another scope.
 
Set a breakpoint in your Update() method. Inspect the value of somebool at the time the breakpoint is hit. Is it the expected value that matches your game data?

More often than not, beginners run across the issue that they don't keep track of the scope of their variables. They expect a variable named the same but in a different scope to automatically update another variable with the same name in another scope.
Problem solved, thank you!

The issue was scope, although I'm still not 100% how. I took the raycast code and made it a method. Then I called this method in the Update method of a different script that I use to handle the game states.
C#:
public void PlayerMovement()
    {
        if (Input.GetMouseButtonDown(0))
        {

            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);


            if (Physics.Raycast(ray, out hit, 100))
            {
                agent.SetDestination(hit.point);
            }
        }
    }
In the playercontroller script

C#:
public void Update()
    {
        if (state == BattleState.PLAN && moveButtonPressed)
        {
            playerController.PlayerMovement();
        }
}
In the battlesystem script

This works perfectly
 
Back
Top Bottom