• 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 Maakaapeli · Nov 22, 2016 at 09:07 AM · c#child objectdropdown

How to access dropdown objects childrens?

Hi! I'm currently working on project, where we want to navigate menus with controller. Problem now is that how I can access and select dropdown menus options? So in scene I have dropdown object with 4 options. Current task is to be able to navigate in those options only with controller.

This script is attached to the Canvas. Canvas has canvas reycaster,canvas scaler,canvas and rect transform components.

Problem lies on a line 106.

Any help is appreciated!

using UnityEngine

using System.Collections

using UnityEngine.EventSystems

using System.Collections.Generic

using UnityEngine.SceneManagement

using UnityEngine.UI

using System.Linq

public class uiNavigation : MonoBehaviour {

 //List for menu options
 public List<GameObject> menuOptions = new List<GameObject> ();

 //Private variable for list
 private int menuIndex;
 private bool canClick;
 private bool canSlide;


 //Just some basic formating for variables
 void Start () {

     canClick = true;
     canSlide = true;

     //Cursor.visible = false;
     //Cursor.lockState = CursorLockMode.Locked;

     if (EventSystem.current.currentSelectedGameObject == null) {
         
         EventSystem.current.SetSelectedGameObject (menuOptions [menuIndex]);

     }
 }

 void Update () {

     int listSize = menuOptions.Count;

     //For Vertical navigation in menus
     if (Input.GetAxisRaw ("p1 controller dpad Y") == 1) {
         //(Input.GetButtonDown("Menu up")
         if (canClick == false) {

             return;

         }

         canClick = false;
         menuIndex--;
         print (menuIndex);


         if (menuIndex < 0) {

             print ("smaller than 0");
             menuIndex = 0;

         }

         EventSystem.current.SetSelectedGameObject (menuOptions [menuIndex]);

     }

     if (Input.GetAxisRaw ("p1 controller dpad Y") == -1) {
         //(Input.GetButtonDown("Menu down")

         if (canClick == false) {

             return;

         }

         canClick = false;
         menuIndex++;


         if (menuIndex > (listSize - 1)) {

             print ("greater than " + (listSize - 1));
             menuIndex = listSize - 1;
             print ("MenuIndex " + menuIndex);

         }

         EventSystem.current.SetSelectedGameObject (menuOptions [menuIndex]);
     }

     if (Input.GetAxisRaw ("p1 controller dpad Y") == 0) {
         
         //(!Input.GetButtonDown("Menu down" || !Input.GetButtonDown("Menu up"))
         canClick = true;

     }

     //What happens on submit button press.
     if (Input.GetButtonDown ("Submit")) {

         if (menuOptions [menuIndex].GetComponent<Button> ()) {

             //Activates button
             menuOptions [menuIndex].GetComponent<Button> ().onClick.Invoke ();

         } else if (menuOptions [menuIndex].GetComponent<Dropdown> ()) {

             //TODO Make this shit work like it shoud be.
             menuOptions [menuIndex].GetComponent<Dropdown> ().Show ();

             //menuOptions [menuIndex].GetComponent<Dropdown> ().GetComponentInChildren<Dropdown.OptionDataList> ();
             //EventSystem.current.SetSelectedGameObject (menuOptions [menuIndex].GetComponent<Dropdown> ().gameObject);
             //menuOptions [menuIndex].GetComponent<GameObject> ().SetActive(true);

         } else if (menuOptions [menuIndex].GetComponent<Toggle> ()) {


             if (menuOptions [menuIndex].GetComponent<Toggle> ().isOn) {

                 print ("toggle is on");
                 menuOptions [menuIndex].GetComponent<Toggle> ().isOn = false;

             } else {
                 
                 print ("toggle is off");
                 menuOptions [menuIndex].GetComponent<Toggle> ().isOn = true;

             }
         }
     }

     //For Slider adjustment
     if (menuOptions [menuIndex].GetComponent<Slider> ()) {


         if (Input.GetAxisRaw ("p1 controller dpad X") == 1) {

             if (canSlide == false) {

                 return;

             }

             canSlide = false;
             menuOptions [menuIndex].GetComponent<Slider> ().value++;

         }

         if (Input.GetAxisRaw ("p1 controller dpad X") == -1) {

             if (canSlide == false) {

                 return;

             }

             canSlide = false;
             menuOptions [menuIndex].GetComponent<Slider> ().value--;
         }

         if (Input.GetAxisRaw ("p1 controller dpad X") == 0) {

             canSlide = true;

         }
     }


 }

}

Comment
Add comment · Show 1
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 Rickywild · Nov 29, 2016 at 05:57 PM 0
Share

transform.GetChild(i)?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

264 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 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 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 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

Using Child Object To Control Parent 0 Answers

Can't find Animator State/Invalid Layer Index in child object inside prefab 0 Answers

Tutorial for Dropdown item needed 1 Answer

Autocomplete Search (Search Suggestion) 5 Answers

change return type of virtual method 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