- Home /
List of gameobjects attached to single object
Hey Guys, I'm making a 2D game. In some point I want that when my character goes through some position I want to 'shot him' with another game object. In other words, I got a hidden GameObject, and when my player walks into some kind of "detector" that hidden GameObject will start moving in the same direction my player is. So, if it touches him, he'll die.
You will see what I mean clearly in this video.
Check seconds 0.10 and 0.15. That's exactly what i want to do.
The issue is that i got that working, but every "detector" (in my case a detector is a BoxCollider2D wich is Trigger) can only have one gameObject (one spike) attached to it.
public class ActivateSlider : MonoBehaviour {
public EnemDesplazController slider; //I have to attach the slider on unity
void OnTriggerEnter2D(Collider2D col){
if (col.name == "Character") {
slider.activated = true;
//this will go to my spike and activate it, so it'll start moving.
this.GetComponent<BoxCollider2D>().enabled = false;
}
}
}
public class EnemDesplazController : MonoBehaviour {
public bool activated = false;
//Got some more variables here but they are related
//to the movement of the spike
// Update is called once per frame
void Update () {
if (activated) {
//Code that moves my spike. I ve already done it.
}
}
void OnTriggerEnter2D (Collider2D col){
Respawn rsp = new Respawn ();
rsp.OnTriggerEnter2D (col);
//This method kills my player when the gameobject (spike) touches it.
}
}
So the question is, can I attach multiple objects to a detector? I mean, I'd like to do smething like this:
public class ActivateSlider : MonoBehaviour {
public EnemDesplazController[] sliderLst; //And then on unity i will drag and drop every single spike.
Maybe there's a better way to do what I want. I'll be listening your advices.
Thanks in advance.
There are many ways to solve this problem. One solution is to put the collider on an empty game object. Have that empty game object then inform each of the 'spike' game object to start moving. You can inform them by 1) sending a message to the objects, 2) directly calling a method on each object, or 3) using Events and Delegates to communicate to the objects. Figure out which and how will take some research on your part.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
2D Animation does not start 1 Answer
Choose random prefabs from list? 1 Answer
2D game animation is running 1 Answer