Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Activation of bool not working

Discussion in 'Scripting' started by Chrubgamer, Sep 2, 2020.

  1. Chrubgamer

    Chrubgamer

    Joined:
    Jan 13, 2019
    Posts:
    3
    Hi,
    I am working on enabling and disabling scripts.
    i have a script called PlayerData(Short=PD) and in that script i have a few boolians called doubleJump, higherJumps and quickWalk(sorry for the bad naming) in an other script i have an method to apply these abilitys on a sertain buttun press. (also, i have two abilitys mainAbility and secondAbility)
    the button press works by haveing a int and if a button is pressed it adds one to the "mainAbility" int. and here is what is going wrong, this is my method:

    Code (CSharp):
    1. public void ApplyScriptsAbility()
    2.     {
    3.         switch (mainAbility)
    4.         {
    5.             case 0:
    6.                 PD.doubleJump = false;
    7.                 PD.higherJumps = false;
    8.                 PD.quickWalk = false;
    9.  
    10.                
    11.  
    12.                 break;
    13.             case 1:
    14.                 PD.doubleJump = true;
    15.                 PD.higherJumps = false;
    16.                 PD.quickWalk = false;
    17.  
    18.              
    19.  
    20.                 break;
    21.             case 2:
    22.                 PD.doubleJump = false;
    23.                 PD.higherJumps = true;
    24.                 PD.quickWalk = false;
    25.  
    26.                
    27.                 break;
    28.             case 3:
    29.                 PD.doubleJump = false;
    30.                 PD.higherJumps = false;
    31.                 PD.quickWalk = true;
    32.  
    33.                
    34.  
    35.                 break;
    36.         }
    37.  
    38.         switch (secondAbility)
    39.         {
    40.             case 0:
    41.                 PD.doubleJump = false;
    42.                 PD.higherJumps = false;
    43.                 PD.quickWalk = false;
    44.  
    45.                
    46.  
    47.                 break;
    48.             case 1:
    49.                 PD.doubleJump = true;
    50.                 PD.higherJumps = false;
    51.                 PD.quickWalk = false;
    52.  
    53.                
    54.  
    55.                 break;
    56.             case 2:
    57.                 PD.doubleJump = false;
    58.                 PD.higherJumps = true;
    59.                 PD.quickWalk = false;
    60.  
    61.              
    62.                 break;
    63.             case 3:
    64.                 PD.doubleJump = false;
    65.                 PD.higherJumps = false;
    66.                 PD.quickWalk = true;
    67.  
    68.  
    69.                 break;
    70.         }
    71.         Debug.Log(mainAbility);
    72.      
    73.     }
    here i just check for mainAbility to be a sertain number and enable/disable bools. I have no idea why but for some reason secondAbility works just fine but mainAbility does not work, am i overseeing something or is it just gliched, and of course if you know how pls leave it down below.

    Thanks in advance,
    Chris
     
    Last edited: Sep 3, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,318
    I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  3. ProphetSword

    ProphetSword

    Joined:
    Apr 11, 2013
    Posts:
    10
    If mainAbility is a float, how are you converting it to an int in order to use it in the switch statement?
     
  4. Chrubgamer

    Chrubgamer

    Joined:
    Jan 13, 2019
    Posts:
    3
    Oh yea i thought i used a float but i am now checking the scipt and it was an int ill change the question and thanks for th heads up!
     
  5. Chrubgamer

    Chrubgamer

    Joined:
    Jan 13, 2019
    Posts:
    3
    tnx for the reply, after i put in all the debugs in i found out that it should just run. the debug statement just runs when the case is true but still nothing is changed. I then thought maybe its someting to do with the second switch, but when i duplicated the secondAbility switch and changed secondAbility to mainAbility. nothing chaned
     
  6. ProphetSword

    ProphetSword

    Joined:
    Apr 11, 2013
    Posts:
    10
    After re-reading your original post, I have to wonder if you know for sure that it's not working.

    Here's why: you first use a switch on mainAbility and set the three bools (for example, let's say you set them to this: PD.doubleJump = false, PD.higherJumps = false and PD.quickWalk = false), and then immediately reset them with secondAbility (so, let's say they become this: PD.doubleJump = true, PD.higherJumps = false and PD.quickWalk = true).

    As a result, you are only seeing what happens to secondAbility; because it immediately changes the bools from whatever mainAbility set them to. So, even though they changed with mainAbility, secondAbility changes them an instant later.

    That could be why it doesn't seem to work, even though it does.

    If you needed to have different settings based upon the two different abilities, they should be set to different bools. What you're doing here is only grabbing what comes from the secondAbility switch.