Enable Multiple Box Collides From Parent Object
I have a box collider acting as a trigger, when the player enters this trigger i'd like to enable the box colliders of the 5 child objects I have, as well as their sprite renderers. This is what I have so far but it's not quite right.
using UnityEngine;
using System.Collections;
public class EnableObject : MonoBehaviour {
private BoxCollider[] boxColliders;
private SpriteRenderer[] sR;
private bool run = false;
void Update()
{
if(run)
{
boxColliders = GetComponentsInChildren<BoxCollider>();
sR = GetComponentsInChildren<SpriteRenderer>();
boxColliders.enabled = true;
sR.enabled = true;
}
}
void OnTriggerEnter(Collider other)
{
if(other.GetComponent<Collider>().tag == "Player")
{
run = true;
}
}
}
both boxcollider[] and spriterenderer[] have no definition for enabled.
Answer by Statement · Nov 07, 2015 at 12:01 AM
boxColliders = GetComponentsInChildren<BoxCollider>();
sR = GetComponentsInChildren<SpriteRenderer>();
So each call return an array of box colliders and sprite renderers.
boxColliders.enabled = true;
sR.enabled = true;
Arrays can't be enabled. BoxCollider and SpriteRenderer can, though.
foreach (var collider in boxColliders)
collider.enabled = true;
foreach (var s in sR)
s.enabled = true;
You have to treat collections and single instances as different types. There's a set of members exposed for arrays, and you have to get an item from the array to perform work on a particular item.
Answer by zellimzel · Feb 10, 2016 at 01:50 AM
Very nice and informative article here. I also can share my experience in files merging. Just look at the service http://goo.gl/ONR66c. Its pretty easy to use.