- Home /
how can i test to see if there are more than one objects with a tag C#
I have 4 objects with a trigger when all four objects (fourth is out of the frame)
i have a trigger around the cubes with this script.
using UnityEngine;
using System.Collections;
public class ColorDetector : MonoBehaviour
{
public GameObject Key;
void OnTriggerEnter (Collider Keyz)
{
if (Keyz.gameObject.tag == "Yellow")
{
if (Keyz.gameObject.tag == "Blue")
{
if (Keyz.gameObject.tag == "Red")
{
if (Keyz.gameObject.tag == "Green")
{
Debug.Log ("IT Worked");
Key.SetActive (true);
}
}
}
}
}
}
and the script does not set the key to active. thanks to all who can help.
Your script is testing if the one colliding object has all four tags on it, is that intentional?
no i want to test each cube individually and if all four are there the it sets the key to active
You'll need to check if the colliding object has any of the tags, then, not every tag. Then keep track of which objects have already collided with it.
This will work
public bool havekeyBlue = false;
public bool havekeyGreen = false;
public bool havekeyRed = false;
public bool havekeyYellow = false;
public GameObject me;
void Start()
{
}
void Update()
{
}
private void OnTriggerEnter(Collider collider)
{
if (collider.CompareTag("Blue$$anonymous$$ey"))
{
havekeyBlue= true;
}
if (collider.CompareTag("Green$$anonymous$$Ey"))
{
havekeyGreen = true;
}
if (collider.CompareTag("Red$$anonymous$$ey"))
{
havekeyRed = true;
}
if (collider.CompareTag("Yellow$$anonymous$$ey"))
{
havekeyYellow = true;
}
}
thanks but i dont want the key to change color i want the trigger to detect that all four cubes are in the trigger then set the key to active.
Answer by SSEthanCray · Jan 28, 2018 at 03:54 AM
Is your debug.log coming out as "it worked"? If it isn't you could handle the code like this. I'm not sure if this is the type of answer you were looking for but here you go. using UnityEngine; using System.Collections;
public class ColorDetector : MonoBehaviour
{
public GameObject Key;
private int colorCounter = 0;
void Update() {
if (colorCounter = 4) {
//setactive blah
}
}
void OnTriggerEnter (Collider Keyz)
{
if (your thing is green) {
colorCounter++;
}
if (your thing is red) {
colorCounter++;
}
// repeat for your colors
}
void OnTriggerExit (Collider Keyz)
{
if (your thing is green) {
colorCounter--;
}
if (your thing is red) {
colorCounter--;
}
//repeat for colors
}
Your answer
Follow this Question
Related Questions
Accessing components via script and assigning randomized values to their parameters? 1 Answer
How to play to animations at same time ? 1 Answer
Scriptable Objects or Scenes ,Dungeon game: Use scens or Scriptable object? 0 Answers
Capture a (screen shot) scene shot with alpha 2 Answers
How do I make a photography function? 0 Answers