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 /
  • Help Room /
avatar image
0
Question by SpaceKing · Oct 05, 2016 at 02:32 AM · arraygetcomponentbooleanfunction call

How do you access the Scripts, Functions, and Bools within an array of GameObjects?

I'm trying to develop a system that allows me to switch out menus when the player presses a button. I already have a HideMenu and ShowMenu function in a class on each menu GameObject, each attached to a button. Whenever these occur, they switch a isShown boolean. However, I want it so that when the player presses another button, it performs HideMenu for any menus for which isShown is active. I can't just use active and not active because I'm tweening them, not disabling them. Each Sliding Menu is its own GameObject, with its own MenuBehavior script.

My idea was this:

  1. Declare an array of GameObjects called slideMenus in a separate MenuManager script.

  2. In start, assign all GameObjects with the tag 'SlideMenu' into this array.

  3. In a SwitchMenu function (called by the buttons), perform a foreach loop for each item in the array.

  4. In the loop, if isShown is true, activate the function HideMenu.

Code:

 public class MenuManager : MonoBehaviour {
     
         private GameObject[] slideMenus;
     
         public void Start()
             {
                 slideMenus = GameObject.FindGameObjectsWithTag("SlideMenu");
             }
     
         public void SwitchMenu ()
             {
                 foreach (GameObject menu in slideMenus)
                 {
                     if (menu.GetComponent<MenuBehaviour>().isShown)
                         menu.GetComponent<MenuBehaviour>().HideMenu();
                 }
             }
         }

The code doesn't present any errors, but in practice I get

"NullReferenceException: Object reference not set to an instance of an object MenuManager.SwitchMenu () (at Assets/Scripts/MenuManager.cs:58) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:630) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:765) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()"

as an error.

Any help would be appreciated.

EDIT: Alternatively, I could do the reverse and make ShowMenu just hide any other menus that are active, but that presents the same kind of problem with accessing the MenuBehaviour of other menus.

Comment
Add comment · Show 2
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 doublemax · Oct 05, 2016 at 07:03 AM 1
Share

First i'd add some Debug.Log outputs to see what's going on. $$anonymous$$ost like the crash occurs because menu.GetComponent() returns null at some point.

Question is: Does it do that for all found menus or for one? $$anonymous$$aybe you have forgotten to add the $$anonymous$$enuBehaviour in one menu? Or you have a tagged an object "Slide$$anonymous$$enu" although it's not a menu.

Just adding Debug.log( menu.name ) inside the loop might already give a hint.

avatar image SpaceKing doublemax · Oct 05, 2016 at 07:40 AM 1
Share

Well now I just feel silly: One of them WAS missing the script. Everything works as expected now, thank you.

0 Replies

· Add your reply
  • Sort: 

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

80 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Storing delegates or function calls in a array 0 Answers

Problem Destroying Component 0 Answers

Changing Variables on one script with multiple other scripts 1 Answer

If ALL in boolean Array = true ... else ... 3 Answers

Randomized array result from other scipt 2 Answers


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