- Home /
Why cant my FindGameObjectsWithTag function find any objects?
I have 3 buttons placed in my scene and I want to find out how many there are in the scene by using FindGameObjectsWithTag("Button") and buttons.Length. But for some reason buttons.Length keeps giving me the value zero even though there are 3 buttons in my scene with the tag "Button".
Here is my code:
using UnityEngine;
using System.Collections;
public class ButtonChecker : MonoBehaviour {
public int amountofButtons;
public GameObject FirstButton;
public ButtonPress ButtonPresser;
public GameObject[] buttons;
void Start() {
ButtonPresser = FirstButton.GetComponent<ButtonPress> ();
if (buttons == null) {
buttons = GameObject.FindGameObjectsWithTag("Button");
}
amountofButtons = buttons.Length;
}
// Update is called once per frame
void Update () {
Debug.Log (buttons.Length);
if (ButtonPresser.activeButtons == amountofButtons) {
//Debug.Log ("All Buttons are pressed!!");
}
else {
//Debug.Log("All not Pressed");
}
}
}
Answer by wibble82 · Dec 22, 2015 at 10:47 AM
Hey
Not sure which, but this will almost certainly be 1 of these few problems:
The buttons are 'disabled', in which case the Find function won't pick them up
The case is wrong in the tag (i.e. they're called button, not Button - easy mistake to make!)
You're confusing 'layer' with 'tag'
As it is a public (serialised) property, 'buttons' is never null, so you never actually do the search - it starts off as a 0 length array and stays that way. Try doing the search regardless of whether it starts null, or even do it every frame to see if that fixes it.
Could be wrong, but check those out first :)
-Chris
Taking out the IF null statement worked! Thanks! I ran in to another problem though. You see the idea with this script is to check all the buttons and to see if all buttons are colliding with something. If all buttons are colliding with something a spinning door should start to rotate (unlock).
Here is the script that handles button collision: using UnityEngine; using System.Collections;
public class ButtonPress : $$anonymous$$onoBehaviour
{
//public GameObject TurningDoor;
//float hoverForce = 0.2f;
//bool CollidingObject = false;
public int activeButtons = 0;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnCollisionEnter (Collision Other)
{
//CollidingObject = true;
activeButtons++;
}
void OnCollisionExit (Collision Other) {
//CollidingObject = false;
activeButtons--;
}
And here is the script which checks the buttons: using UnityEngine; using System.Collections;
public class ButtonChecker : $$anonymous$$onoBehaviour {
public int amountofButtons;
public GameObject FirstButton;
public ButtonPress ButtonPresser;
public GameObject[] buttons;
float rotationForce = 0.1f;
void Start() {
ButtonPresser = FirstButton.GetComponent<ButtonPress> ();
buttons = GameObject.FindGameObjectsWithTag("Button");
amountofButtons = buttons.Length;
}
// Update is called once per frame
void Update () {
Debug.Log (buttons.Length);
if (ButtonPresser.activeButtons == amountofButtons) {
transform.Rotate( new Vector3(0, 5, 0) * rotationForce, Space.Self );
}
}
}
The problem is that the Int "activeButtons" only adds by one if the first button is colliding with something and ignores the other 2 buttons. All buttons are prefabs btw.
Hi
It's important to use the forums correctly to help other people find answers to questions and see what is right/wrong. So could I ask you to:
$$anonymous$$ark the question as 'correct' if it is correct (using the 'accept' button)!
Ask new questions as actual new questions
If you could fix those up, I'll have a think about your 2nd question :)
cheers
-Chris
(and feel free to post a link to your new question in these comments - I'll keep an eye out for it!)
Answer by GioVil · Dec 22, 2015 at 10:41 AM
Hi, are you sure you have the objects with "button" Tag and not with "Button" Layer?
Your answer

Follow this Question
Related Questions
Is there something like clicked.gameObject? 3 Answers
Storing tag strings in an array, from an already existing array of gameobjects with different tags. 1 Answer
OverlapSphere, check how many object with specific tag did it collide with.. 2 Answers
Why is this Raycast not working ? 3 Answers
Physics.CheckSphere with tag 1 Answer