- Home /
How do I spawn something if I'm touching something else in an array of children?
I have an array of game objects (with circle colliders set to triggers) positioned equidistant from each other in a circle pattern. A player object will move in the direction of the circle pattern constantly. I want to be able to calculate when the player is touching one of the colliders and only spawn something on the opposite side of the circle pattern or somewhere else to avoid spawning something exactly where the player object happens to be.
These objects are in an array, but they spawn as a child of a parent object in the scene. The script is on the parent object. I can't seem to figure out how to code the trigger interaction between the children and the player object.
I only need help with the "If the player is colliding with this particular trigger, then do something over there" logic. I can figure out how actually spawn things or timings with coroutines and all that.
For reference the first array object (SpawnPoint 0) spawns at the top of the circle, and each following objects spawns at 30 degree intervals counter-clockwise.
public class SpawnManager : MonoBehaviour
{
//circle spawner variables
private int totalSpawners;
public GameObject circleSpawner;
private GameObject[] circleArray;
//weapons spawn variables
private bool canSpawnWeapon = false;
private bool weaponHasSpawned = false;
//coin spawn variables
private bool canSpawnCoin = false;
private bool coinHasSpawned = false;
public GameObject player;
public GameObject weapon;
public GameObject coin;
private void Awake()
{
totalSpawners = 12;
}
public void CircleSpawner()
{
circleArray = new GameObject[totalSpawners];
for (int i = 0; i < circleArray.Length; i++)
{
float angle = 30 * i;
Vector2 rotator = new Vector2(Mathf.Sin(Mathf.Deg2Rad * angle), Mathf.Cos(Mathf.Deg2Rad * angle)) * PlayerControl.orbitRadius;
GameObject spawnerOnCircle = Instantiate(circleSpawner, rotator, Quaternion.identity) as GameObject;
spawnerOnCircle.name = "SpawnPoint" + i.ToString();
spawnerOnCircle.transform.parent = this.transform;
circleArray[i] = spawnerOnCircle;
}
}
}
Answer by Trevdevs · Jun 24, 2018 at 11:58 PM
Add a script to your object with the OnTriggerEnter function make sure this script is attached to the object with the collider or it will not work, also make sure the player has a rigidbody as well.
Your answer
![](https://koobas.hobune.stream/wayback/20220612165411im_/https://answers.unity.com/themes/thub/images/avi.jpg)