Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Onclick event for prefab button not working... HELP.

Discussion in 'Scripting' started by jcfrizz, May 2, 2018.

  1. jcfrizz

    jcfrizz

    Joined:
    Jul 22, 2017
    Posts:
    11
    Disregard this thread. I found a solution!

    Okay so in my game I'm currently working on, I have a list of buttons. The list of buttons is really long so I have dynamically created the list using an object pool, and they only fill up the list when I run the game. There is a prefab of these buttons though that I am trying to work with. The issue is, I want to be able to have a pop up come up each time one of the buttons are clicked. Right now, I tested it with a debug log, and the code is reaching this point, but the popup is not actually coming up. I have attached my image to the variable in the inspector and my button prefab to the inspector variable.

    I am aware that prefabs don't work well when it comes to this stuff, but I have no idea how else to go about this. Any help is much appreciated. Here is the basic code for trying to activate my pop up image:
    Code (CSharp):
    1. public Image popUpImage;
    2. public Button button;
    3.  
    4. void Start ()
    5. {
    6.     popUpImage.enabled = false;
    7.     button.onClick.AddListener (activatePopUp);
    8. }
    9.  
    10. public void activatePopUp()
    11. {
    12.     popUpImage.enabled = true;
    13.     Debug.Log("pop up is active");
    14. }
    And if you are curious to see, here is my code for my object pool. I found it online in one of the tutorials, but it's worked really great so far with my code. Here it is:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. // A very simple object pooling class
    7. public class SimpleObjectPool : MonoBehaviour
    8. {
    9.  
    10.     // the prefab that this object pool returns instances of
    11.     public GameObject prefab;
    12.     // collection of currently inactive instances of the prefab
    13.     private Stack<GameObject> inactiveInstances = new Stack<GameObject>();
    14.  
    15.  
    16.     // Returns an instance of the prefab
    17.     public GameObject GetObject()
    18.     {
    19.         GameObject spawnedGameObject;
    20.  
    21.         // if there is an inactive instance of the prefab ready to return, return that
    22.         if (inactiveInstances.Count > 0)
    23.         {
    24.             // remove the instance from the collection of inactive instances
    25.             spawnedGameObject = inactiveInstances.Pop();
    26.         }
    27.         // otherwise, create a new instance
    28.         else
    29.         {
    30.             spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
    31.  
    32.             // add the PooledObject component to the prefab so we know it came from this pool
    33.             PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
    34.             pooledObject.pool = this;
    35.         }
    36.  
    37.         // put the instance in the root of the scene and enable it
    38.         spawnedGameObject.transform.SetParent(null);
    39.         spawnedGameObject.SetActive(true);
    40.  
    41.         // return a reference to the instance
    42.         return spawnedGameObject;
    43.     }
    44.  
    45.     // Return an instance of the prefab to the pool
    46.     public void ReturnObject(GameObject toReturn)
    47.     {
    48.         PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
    49.  
    50.         // if the instance came from this pool, return it to the pool
    51.         if(pooledObject != null && pooledObject.pool == this)
    52.         {
    53.             // make the instance a child of this and disable it
    54.             toReturn.transform.SetParent(transform);
    55.             toReturn.SetActive(false);
    56.  
    57.             // add the instance to the collection of inactive instances
    58.             inactiveInstances.Push(toReturn);
    59.         }
    60.         // otherwise, just destroy it
    61.         else
    62.         {
    63.             Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
    64.             Destroy(toReturn);
    65.         }
    66.     }
    67. }
    68.  
    69. // a component that simply identifies the pool that a GameObject came from
    70. public class PooledObject : MonoBehaviour
    71. {
    72.     public SimpleObjectPool pool;
    73. }
    If anyone knows ANY way for me to get this to work with me using the button object pool, please let me know. Even if I have to do it manually, that's okay. I'm still fairly inexperienced when it comes to this stuff. Thanks in advance!
     
    Last edited: May 2, 2018