Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Schytheron · Dec 22, 2015 at 09:11 AM · tagbuttonscheckarray of gameobjectslength

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");
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Schytheron · Dec 22, 2015 at 01:03 PM 0
Share

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.

avatar image wibble82 Schytheron · Dec 22, 2015 at 01:11 PM 0
Share

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

avatar image wibble82 wibble82 · Dec 22, 2015 at 01:13 PM 0
Share

(and feel free to post a link to your new question in these comments - I'll keep an eye out for it!)

Show more comments
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges