So I have multiple gameobjects that needs to be interactable and I've put them all in one gameobject container. But the thing is I still have to attach the script into each of the gameobjects that needs to be interactable for it to work and that seems kinda iffy when a scenario comes that I have to add more gameObjects.

Is it possible to just put the script in the container object? I tried this on my container object and I'm pretty sure I'm doing something wrong.

public GameObject spriteHandler;
GameObject sprite1;
GameObject sprite2;
GameObject sprite3;
private void Start()
    sprite1 = spriteHandler.transform.GetChild(0).gameObject;
    sprite2 = spriteHandler.transform.GetChild(1).gameObject;
    sprite3 = spriteHandler.transform.GetChild(2).gameObject;
void OnMouseDown()

  • 1
    \$\begingroup\$ What behaviour would you like the parent script to have? Right now this looks pretty placeholder, so it's hard to guess where you're planning to develop it from here. \$\endgroup\$ – DMGregory Dec 10 '20 at 0:17
  • \$\begingroup\$ Basically what's going to be in OnMouseDown() is a method from another script that opens up the dialogue when you click the sprite. That works when I assign the script with OnMouseDown() on the gameobjects individually. I'm just looking for a way so I only need to assign this script with OnMouseDown() on the parent object. Maybe I should use events? \$\endgroup\$ – lover of the feet Dec 10 '20 at 1:16
  • \$\begingroup\$ And is it the same dialogue no matter which sprite you click, or a different dialogue depending on the sprite? Be sure your question contains a complete description of what you need, so you don't get incomplete answers. \$\endgroup\$ – DMGregory Dec 10 '20 at 1:17
  • \$\begingroup\$ My bad. Yeah, it's basically the same dialogue. What sprite you click doesn't matter. \$\endgroup\$ – lover of the feet Dec 10 '20 at 1:18

It depends on your overall goals for what the gameobject is doing. But from what you have said - the preferred method would be to make the Gameobjects prefabs - and attach the interactable script to the prefabs (so as you Instantiate them - each one will have the script attached.).

Here is the documentation on Prefabs & Instantiating Prefabs:

https://docs.unity3d.com/Manual/Prefabs.html https://docs.unity3d.com/Manual/InstantiatingPrefabs.html


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.