Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    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:
    20
    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:
    100
    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