I have been trying to disable isTrigger
in unity c# script on all GameObjects containing a certain tag. Currently my code works and disables the isTrigger part of the box collider, but it only does this for the first object with the tag I am looking for do this for all objects.
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DefenseCollisionTrigger : MonoBehaviour {
public static Rigidbody rb;
public bool CanBePlaced;
public GameObject[] defenseTriggers;
void Start ()
{
CanBePlaced = true;
defenseTriggers = GameObject.FindGameObjectsWithTag("Defense");
}
public void OnTriggerEnter (Collider other)
{
if (other.tag == "Defense")
{
CanBePlaced = false;
}
}
public void OnTriggerExit(Collider other)
{
if (other.tag == "Defense")
{
CanBePlaced = true;
}
}
public void IsTriggerFalse()
{
foreach (Object dt in defenseTriggers)
{
Debug.Log("working on it");
GetComponent<Collider>().isTrigger = false;
}
}
}
That is what is currently looks like. The public void IsTriggerFalse()
is called on each time I go to instantiate an object with my mouse button. The Debug.Log is working and printing to the console. This is the closest I have came to making it work, but it only works for the first object. I have searched for hours and watched videos/looked at unity docs on foreach statements with no luck to my issue. I have also tried using foreach (GameObject dt in defenseTriggers)
instead of just Object.
Anyone know how to solve my issue?