- Home /
True/False isTrigger for Collider array?
I'm trying to set the triggers for an array of colliders to false once the player beats a level. Here is my code:
public class PlayerController : MonoBehaviour {
public float speed;
public Text winText;
public GameObject nextLevel;
public GameObject[] obstacles;
public Collider[] obstacleTrigger;
private Rigidbody rb;
private int count;
private int pickups;
private GameObject[] cubes;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
nextLevel.SetActive(false);
cubes = GameObject.FindGameObjectsWithTag("Pick Up");
pickups = cubes.Length;
obstacles = GameObject.FindGameObjectsWithTag("Obstacle");
for (int i = 0; i < obstacles.Length; i++)
{
obstacleTrigger = obstacles[i].GetComponents<Collider>();
}
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical);
rb.AddForce(movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
if (other.gameObject.CompareTag("Obstacle"))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
void SetCountText()
{
if (count >= pickups)
{
winText.text = "You Win!";
nextLevel.SetActive(true);
obstacleTrigger.isTrigger = false;
}
}
}
What can I use instead of isTrigger to set all of my obstacleTrigger's Is Trigger to false?
isTrigger = false; is the best method i think, just loop all objects and make them false. Or i don't get your question?
I should've mentioned that I'm very new to Unity.
How would I go about looping all the objects and making them false?
$$anonymous$$y goal here is to make a number of objects have their trigger toggle turned off once the player beats the level. Using isTrigger apparently doesn't work with an array of objects.
Wow, actually I got it now thanks to you. This code did it:
void SetCountText()
{
if (count >= pickups)
{
winText.text = "You Win!";
nextLevel.SetActive(true);
foreach (Collider obstacleTriggers in obstacleTrigger)
{
obstacleTriggers.isTrigger = false;
}
}
}
Thanks again man!
Your answer
![](https://koobas.hobune.stream/wayback/20220612164551im_/https://answers.unity.com/themes/thub/images/avi.jpg)