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

Boolean Arrays not working at all

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Mar 8, 2015.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    We are trying to create a building system dependant on resources, this worked fine.
    After realising not all our buildings are going to have a set amount of pieces and may be different, we tried to convert the whole script to use an Array, long story short...It doesn't work.

    more than that Debug.Log doesn't seem to work anywhere, not even in the Update function.
    Code is below:
    Code (JavaScript):
    1. #pragma strict
    2. import UnityEngine.UI;
    3.  
    4. private var material : GUIBuild;
    5. private var canBuild : boolean = false;
    6.  
    7. var BuildText : Text;
    8. //var TextToUse = "";
    9. private var drawGUI = false;
    10.  
    11. //var seg1 : boolean = false;
    12. //var seg2 : boolean = false;
    13. //var seg3 : boolean = false;
    14.  
    15. var segments : boolean[] = BooleanArrayTrue(100);
    16. var counter : int = 0;
    17. function BooleanArrayTrue (size : int) : boolean[] {
    18.      var boolArray = new boolean[size];
    19.      for (var b in boolArray) b = false;
    20.      return boolArray;
    21. }
    22.  
    23. function Start()
    24. {
    25.     material = GameObject.FindWithTag("Player").GetComponent(GUIBuild);
    26.     /*for(var i : int = 0; i < 100; i++)
    27.     {
    28.         segments[i]=false;
    29.     }*/
    30.     segments[0] = true;
    31. }
    32.  
    33. function OnTriggerEnter (Col : Collider)
    34. {
    35.     if(Col.tag == "Player")
    36.     {
    37.         canBuild = true;
    38.         drawGUI = true;
    39.     }
    40. }
    41.  
    42. function OnTriggerExit (Col : Collider)
    43. {
    44.     if(Col.tag == "Player")
    45.     {
    46.         canBuild = false;
    47.         drawGUI = false;
    48.     }
    49. }
    50.  
    51. function Update()
    52. {
    53.  
    54.     if (drawGUI == true)
    55.         {
    56.             BuildText.text = "Press B To Build";
    57.         }
    58.     else
    59.         {
    60.             BuildText.text = "";
    61.         }
    62.  
    63. if(canBuild)
    64.     {
    65.         if(Input.GetKeyDown("b"))
    66.         {
    67.             for(var i : int = 0; i < segments.Length; i++)
    68.             {
    69.             if((material.currWood >= 1 && material.currMetal >= 1 && material.currBrick >= 1) && segments[counter])
    70.             {
    71.                 var temp = "B" + counter;
    72.                
    73.                 Debug.Log (temp);
    74.                 GameObject.Find("B" + counter).GetComponent(MeshRenderer).enabled = true;
    75.                
    76.                 //Set materials to zero
    77.                 material.currWood--;
    78.                 material.currMetal--;
    79.                 material.currBrick--;
    80.                
    81.                 //segments[counter] = false;
    82.                 segments[counter+1] = true;
    83.             }
    84.             }
    85.         }
    86.     }
    87. }
     
  2. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    if this doesnt work
    Code (CSharp):
    1. function Update () {
    2.   Debug.Log("something");
    3. }
    Then i'd suggest checking the component is actually on a gameobject in the scene

    Other than that, a couple of things you need to check:
    is canbuild being set to true?
    you never assign value of 'counter' (your for loop uses 'i')
    BooleanArrayTrue sets all to false, and by default, a new bool[] is already all false
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Try to Debug.Log your counter variable. Having a short look at the script it appears to not be incremented as intented.
     
  5. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Debug.Log does not work anywhere in this script, I was originally trying to modify this script:

    And decided to add everything as an Array, this would be much more flexible for us, but after doing everything we needed to set it as an array, nothing worked, it's like the Array exists, but nothing thinks it does.
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Have you turned off console messages?