Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why are my booleans true and false?

Discussion in 'Scripting' started by ardizzle, Jul 6, 2014.

  1. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    I have it in two diffrent projects now where i can set a bool in a function to true. Debug.Log(myVar) shows true and then goes back to the function that called that function and right under the call I Debug.Log(myVar) again and it shows false. What is going on?
     
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
  3. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    I understand how the scope works, reread just to make sure i wasn't missing something. But that isn't my problem because I declared the variable at the beginning of the class, due to the fact i knew i would have to use it across many different functions.
     
  4. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Perhaps you could post the relevant code
     
  5. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GenerateDungeon : MonoBehaviour
    5. {
    6.     public int mapXSize; // The max size of tiles along the X axis
    7.     public int mapZSize; // The max size of tiles along the X axis
    8.     public int randomStart; // The position in the listOfTiles array the program starts to look for tiles
    9.     // For example if you don't want your start to come up at random times then set randomStart at 1 and put your start tile above it.
    10.     public GameObject[] listOfTiles;
    11.     private TileLocations locations;
    12.     public bool[] paths;
    13.     void Start ()
    14.     {
    15.         locations = new TileLocations(mapXSize, mapZSize);
    16.         paths = new bool[4]; // Order is up, down, left, right
    17.         PlaceTile();
    18.     }
    19.  
    20.     public void PlaceTile()
    21.     {
    22.         // Puts the tile into its location on the grid.
    23.         int currX = 0;
    24.         int currZ = 0;
    25.         float tileXPos = 0;
    26.         float tileZPos = 0;
    27.         Vector3 startPos = gameObject.transform.localPosition;
    28.         while(currZ != mapZSize)
    29.         {
    30.             while(currX != mapXSize)
    31.             {
    32.                 tileXPos = (float)(currX * 2.5);
    33.                 tileZPos = (float)(currZ * 2.5);
    34.                 GetTilesClose(currX, currZ);
    35.                 Debug.Log (paths[2]); // this prints false
    36.                 GameObject tile = GetTile(paths);
    37.                 Instantiate(tile, new Vector3(startPos.x + tileXPos, 0, startPos.z + tileZPos), tile.transform.rotation);
    38.                 currX++;
    39.             }
    40.             currZ++;
    41.             currX = 0;
    42.         }
    43.  
    44.     }
    45.  
    46.     public void GetTilesClose(int x, int z)
    47.     {
    48.         Debug.Log(x);
    49.         if(x - 1 >=0)
    50.         {
    51.             if(locations.tile[x-1, z] != null)
    52.             {
    53.                 paths[2] = locations.tile[x-1, z].GetComponent<TileSettings>().tileStats.rightOpen; // gets the tile right
    54.                 Debug.Log(paths[2]);// prints true
    55.             }
    56.             else
    57.             {
    58.                 paths[2] = true;//randomBool();
    59.                 Debug.Log ("random");
    60.             }
    61.         }
    62.     }
    63.     public bool randomBool()
    64.     {
    65.         double randNum = Random.value;
    66.         if(randNum <= .5)
    67.         {
    68.             return true;
    69.         }
    70.         else
    71.         {
    72.             return false;
    73.         }
    74.     }
    75. }
    76.  
    77. Its pretty long and confusing so ill try to cut it down to the relevent stuff
     
    Last edited: Jul 7, 2014
  6. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,679
    You seem to be missing a '}' before line 62
     
  7. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    yea just cut out alot of the irrelevent stuff. hit a bracket on accident lol
     
  8. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,679
    You are also not showing the return value for that method.
     
  9. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    You're setting paths to the return value of getPathsClose even though it's a class variable and being set inside that function. At best, it's pointless. At worst it's confusing and will do weird things. Especially since there's no return value from that function.
     
  10. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    sorry suppose to return paths at the end. accidentally took it out to. And i know it was useless but i was trying it to see if it would solve the problem. I tried returning it back to the way it was originally where the GetTilesClose() is void and above changed it to where it just calls the functions but it still prints one of them true and the other false. Will edit code above to take out the redundency and match my current code.
     
  11. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    I don't see anything obviously wrong at the moment. It's possible you've removed the thing that causes the problem when you simplified it for posting here.

    If this were my project, I'd separate the logic out and make it so I can test it all separately. I'd also avoid using class variables for the paths info, and just return it instead. That gives less opportunity for other things to change it and thus simplifies the logic.
     
  12. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    Your right it I did accidentally remove the problem. I had put the wrong number in one of the path arrays. Thanks for the help and sorry for wasting your time. And that does make sense thanks you.
     
  13. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    No problem. Glad you found the issue!