Search Unity

Bool resets for no reason(?)

Discussion in 'Scripting' started by PrieDayThor, Jul 17, 2015.

  1. PrieDayThor

    PrieDayThor

    Joined:
    Jun 18, 2015
    Posts:
    16
    Hi guys,

    I'm currently working on the prim algorithm and got a problem.
    Heres my code: http://pastebin.com/a7AJkjEr

    So atm I'm filling a list of walls and instantiate some testobject and if my wall has it's boolean wallInMaze = false; the method should instantiate just a ground object.
    But currently when I call the function createMaze() all booleans are true even though I change one of them with pickRandomWall() to false.
    I dont know why it does this. Can somebody help me pls?

    greetings
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Posting the code here is generally better.

    One cheap way to debug this stuff is to turn your bool into a property and add in some Debug.Log statements so you can see every time it changes.
     
    landon912 likes this.
  3. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    i didnt look at everything, but I see this
    Code (CSharp):
    1.                 public void setIsNoWallInMaze()         //Makes the wall invisible in the maze
    2.                 {
    3.                         wallInMaze = !wallInMaze;
    4.                 }
    5.  
    doing !wallInMaze does not mean that wallInMaze = false. Instead what it does is it takes what ever wallInMaze is, lets say it is false, and it makes it the opposite, which is true.
    And then the next time you call it, since its now true, !true is false... and then !false is true, etc...

    Try doing wallInMaze = false; and see if it gets rid of your bug.

    I am also not sure as to how you are debugging to see all the walls bool state, but I see here
    Code (CSharp):
    1.    
    2. if(wallList[tmpCounter].isWallInMaze())         // Does stuff when the wall is visible in the Maze;
    3.    {
    4.         wallList[tmpCounter].showState();
    5.          ...............
    So if that is how you are debugging, then of course you will see all is true, because any that is false will not reach the debug code.
     
    Last edited: Jul 17, 2015