Search Unity

listeners not working on dont destroy on load object

Discussion in '2D' started by Mattattack2531, Jan 20, 2019.

  1. Mattattack2531

    Mattattack2531

    Joined:
    Nov 10, 2018
    Posts:
    5
    Never used listeners before so I might just be not understanding how the work properly? But what's happening, is I add a listener to a button every time a specific scene is loaded.

    Here is a list of events that stop it working: -When I first run the game, the listener works as intended, the buttons are created in the script, and the listener is added. -I change the scene, not destroying the object with the script containing the listener. -When the scene changes back, the script will re-create the buttons, and re-add the listener to the buttons. -Using a simple log to check the method the listener should run, the log will no longer show.

    Where I create the listeners!
    Code (CSharp):
    1. foreach (Sprite texture in spriteImages) {
    2.            
    3.             //Creates a button
    4.             GameObject button = Instantiate (shopButtonPrefab) as GameObject;
    5.             // gets the buttons image compnent for changing
    6.             Image buttonImage = button.GetComponent<Image> ();
    7.             Image[] images = button.GetComponentsInChildren<Image>();
    8.  
    9.             int newIndex = spriteIndex;
    10.             button.GetComponent<Button> ().onClick.RemoveAllListeners ();
    11.             button.GetComponent<Button> ().onClick.AddListener (() => ChangePlayerSkin (newIndex));
    12.             spriteIndex++;
    13.             foreach (Image image in images) {
    14.  
    15.                 if (image != buttonImage) {
    16.                     image.sprite = texture;
    17.                     // Uses a bit shift to check if it has been bought
    18.                     button.transform.GetChild (2).gameObject.SetActive (false);
    19.                     if((skinAvailability & 1 << index) == 1 << index)
    20.                     {
    21.                         //Turns off overlay for unbought item
    22.                         button.transform.GetChild (1).gameObject.SetActive(false);//shopButtonContrainer.transform.GetChild (currentSkinIndex).GetChild (1).gameObject.SetActive (false);
    23.                         skinPurchased[index] = true;
    24.                     }
    25.  
    26.                     if (currentSkinIndex+1 == spriteIndex) {
    27.  
    28.                         button.transform.GetChild (2).gameObject.SetActive (true);
    29.                     }
    30.                     index++;
    31.                     break;
    32.                 }
    33.                
    34.                 button.transform.SetParent (shopButtonContrainer.transform, false);
    35.                 button.transform.GetChild (1).GetChild(0).GetComponent<Text> ().text = (skinCosts[spriteIndex-1].ToString());
    36.                 shopButtons.Add (button);
    37.             }
    38.  

    What the listener should call:
    Code (CSharp):
    1. private void ChangePlayerSkin(int index){
    2.  
    3.    selectedSkinIndex = index;
    4.    Debug.Log ("noo");
    5.    if (skinPurchased [index]) {
    6.        currentSkinIndex = index;
    7.  
    8.        foreach (GameObject button in shopButtons) {
    9.  
    10.            button.transform.GetChild (2).gameObject.SetActive (false);
    11.        }
    12.        shopButtons [currentSkinIndex].transform.GetChild (2).gameObject.SetActive (true);
    13.  
    14.    }
    15. }
    // Stops working after the scene is re-loaded
    Only trying to show relevant code. The buttons are created in OnSceneLoaded method every time a certain scene is run. Then the listener is added. Why will the listener only work the first time the scene is loaded? And not every time the scene is reloaded. Does the dontdestroyonload mess up the listener?
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    i've noticed that unity loses references occasionally when you modify code without restarting the program. i'm not sure what exactly is causing it (it was happening to me before i added listeners through code i believe), but i just restart the program in between each change
     
  3. Mattattack2531

    Mattattack2531

    Joined:
    Nov 10, 2018
    Posts:
    5
    Nice Idea, unfortunately did not fix it.
     
  4. Mattattack2531

    Mattattack2531

    Joined:
    Nov 10, 2018
    Posts:
    5
    Still can't fix