Search Unity

3 strange behaviors, i need Help

Discussion in 'Getting Started' started by Stradmann, Jan 23, 2019.

  1. Stradmann

    Stradmann

    Joined:
    Jun 11, 2018
    Posts:
    38
    Hi, I've some problems with this code, and i need help. First of all, in the first for loop, where i add listeners, i send "i" parameter to the other function, but ever send "6" value, who is the value of "charactersToSelect.Length". The socond thing it's going wrong is tha when i click the toggle button it call the function hundreds or thousand times, and the third thing is that when i disable this toggle button, they don't appear with the disabled color i set in editor. So i need help to solve this strange behaviors couse i don't see in the code what's wrong.

    Code (CSharp):
    1.  public void MisionButtonReaction(Mision thisMision)
    2.     {
    3.         misionDescriptionPanel.SetActive(true);
    4.         misionDescriptionText.text = thisMision.misionDescription;
    5.         misionPicture.sprite = thisMision.misionImage;
    6.         areaPicture.sprite = thisMision.misionArea.areaImage;
    7.         areaNameText.text = "Districte " + thisMision.misionArea.areaName;
    8.         if (gameRecord.misionsAsigned.ContainsKey(thisMision))
    9.         {
    10.             destinationCameraController.SetUpDestinationCamera(gameRecord.misionsAsigned[thisMision]);
    11.         }
    12.         availableCharText.text = "0/" + thisMision.maxCharactersAvailable;
    13.         for (int i = 0; i < charactersToSelect.Length; i++)
    14.         {
    15.             charactersToSelect[i].onValueChanged.AddListener(delegate { CharacterSelectionToggleReaction(thisMision.maxCharactersAvailable, i); });
    16.         }
    17.        
    18.     }
    19.  
    20.     public void CharacterSelectionToggleReaction (int maxChar, int index)
    21.     {
    22.         Debug.Log("running function" + index);
    23.         int characterCount = 0;
    24.         for (int i = 0; i < charactersToSelect.Length; i++)
    25.         {
    26.             if (charactersToSelect[i].isOn)
    27.             {
    28.                 characterCount++;
    29.             }
    30.         }
    31.         availableCharText.text = characterCount + "/" + maxChar;
    32.         if (characterCount == maxChar)
    33.         {
    34.             for (int i = 0; i < charactersToSelect.Length; i++)
    35.             {
    36.                 if (!charactersToSelect[i].isOn)
    37.                 {
    38.                     charactersToSelect[i].enabled = false;
    39.                 }
    40.             }
    41.         }
    42.         else
    43.         {
    44.             for (int i = 0; i < charactersToSelect.Length; i++)
    45.             {
    46.                 charactersToSelect[i].enabled = true;
    47.             }
    48.         }
    49.     }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You never use 6 in your for loop because you are checking for "i < charactersToSelect.Length". If charactersToSelect.Length equals 6, then it will only loop as high as 5. But I'm pretty sure you don't want to ever use 6 because that would cause an index out of range error on lines 26 or 36.

    I don't know what you mean by your second issue. What method even gets called by the toggle button here? On the third issue, what are you even referring to here as "this toggle button"? I don't see any actual toggle buttons in your code, unless you've named them something non-descriptive.
     
  3. Stradmann

    Stradmann

    Joined:
    Jun 11, 2018
    Posts:
    38
    I named them something non-descriptive: "charactersToSelect", :oops:
    Anyway... I solved the three issues..
    The first one in line 15 i solved adding line int j = i, and sending j instead of i as a parameter.. then it works, but i don't know exatly why..
    The second one, is that the MisionButtonReaction is called from Update inside a not "well closed" conditional, so i added a new bool who becomes false at the end of adding listeners to make some kind of door. This part is not visible in this piece of code
    And the third one, is that i had to acces to interactable property of the button, not enabled.
    Thanks a lot anyway!