- Home /
Connect 2 gameobjects to interact with them simultaneously?
Hi,
I have 2 separate gameobjects which I want want to highlight at the same time when the cursor is hovering over just one of them. Is there a way to connect them/their colliders within Unity?
Thanks in advance!
This should be straightforward enough but just need a bit of extra info if that's ok. At what point would you want to connect the 2 gameobjects together? If this something that would be setup in the scene before the game runs or is this something that would happen during the runtime of the game?
Answer by mythirdalias · Jan 15, 2018 at 12:04 PM
Here is if you want to link just two objects together:
public GameObject connectedObject;
Then add the other GameObject in the inspector. This is for if you want 3 or more, same process just add the relevant GameObjects in the inspector:
public class LinkedGameObjects : MonoBehaviour
{
public List<GameObject> gameObjectList = new List<GameObject>();
OnColliderEnter()
{
foreach(GameObject g in gameObjectList)
{
// Put whatever your logic is here. I assume g.material = new Material(). //
}
}
}
Answer by agray427 · Jan 15, 2018 at 05:45 PM
You might also want to look at delegates and events, but this wouldn't be the best solution in every situation. This would really only be an option if you didn't want to just arbitrarily connect objects together that may or may not have anything in common. Likewise, it would decouple any references and would be the better way to handle this if the game objects were to be spawned into the scene. Mythirdalias is right on the money, this is just another way to go about doing it.