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

How to disable an object when you have many clones?

Discussion in '2D' started by Goldsters, Sep 16, 2018.

  1. Goldsters

    Goldsters

    Joined:
    Sep 7, 2018
    Posts:
    3
    I have an object pool set up to instantiate 30 clones of a button. See images below...
    upload_2018-9-16_20-46-13.png upload_2018-9-16_20-47-43.png

    As time goes on, the game then activates these buttons and they then position themselves at the top of the screen and fall down to the bottom. The user needs to tap on them to deactivate them before they hit the bottom of the screen.

    My problem here is when I use gameObject.SetActive(false) in the script that holds each buttons OnClick method, it deactivates ALL of the buttons in the pool. I want to only deactivate the button that the user has clicked on.

    I have tried using getSiblingIndex() to find the index that the button is in and then disable that index value, but since my buttons are prefabs, I cannot access the siblingIndex value from my InstantiateMeteor script.
     
  2. lipisis

    lipisis

    Joined:
    Jan 14, 2017
    Posts:
    37
    Most likely your architecture design is wrong. You have "one script that holds each buttons OnClick method". That already sounds wrong. I doubt you have 30 different methods for the same action basically.

    The easiest way for such situation is to have "Meteor" prefab that has script "MeteorManager" attached to himself and it has a method to disable himself (the object that the script is attached to). You can achieve that with your given function
    gameobject.SetActive(false)
    . At the end you should have 30 instances of meteors that each of them has its own script.

    If you want to have reference to all meteors in one script, just create an array of "MetorManager" and then you will be able to refer to any meteor you like.
     
  3. Goldsters

    Goldsters

    Joined:
    Sep 7, 2018
    Posts:
    3
    My "MeteorManager" script requires access to many other scripts and since it is on a prefab, you cannot put components on them.

    For example, my pooled buttons are set as children to a panel
    obj.transform.SetParent(meteorContainer);
    I can't put the panel component on the script in the Inspector if the script is on a prefab.
     
  4. Goldsters

    Goldsters

    Joined:
    Sep 7, 2018
    Posts:
    3
    Ah, I fixed it. I needed my OnClick function to be on a script on my button prefab, then I needed to activate my button onClickListener
    GetComponent<Button>().onClick.AddListener(() => OnMeteorClick());
    in the start function.
     
  5. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    254
    You could put a gameobject with a triggered collider at the bottom of the field, so when the meteor collider triggers it, the meteor destroy itselfs

    Code (CSharp):
    1.  void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if (other.gameObject.CompareTag("Meteor"))
    4.         {
    5.          
    6.          
    7.                 Destroy(gameObject);      
    8.  
    9.         }
    10.     }
    PD: Post No. 69. lel