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 lazerblade01 · Nov 22, 2016 at 09:07 AM · scripting problemuioutline

UI Outline Enable / Disable via Script?

So I have a basic main menu and subsequent sub-menu. I have pointerEnter set to enable an outline on my text, to show that it's actively "selected". I have pointerExit set to disable outline on exit.

I have my menu script attached to the canvas, where all of the buttons / text live. I have the two panels (main menu, options) set to groups in my script so i can show / hide said panels when the appropriate button is clicked (options button, back button).

I want to deselect (turn off the outline) of a button when it's clicked, so that returning to that screen shows no button currently selected.

I have tried numerous things to see if I could get my script working. At this point my only option is to set each and every button to a variable, but I was hoping I could just grab all outlines (or grab all buttons and find their text / outline component), then turn them off either in a loop (foreach component outline in menugroup) or simply by telling unity to turn off all outlines (kind of like how jQuery can affect all objects in an array at once).

I'm strictly using C# for this learning project, so if you can point me in the right direction, it'd be very helpful.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class menuScript : MonoBehaviour
 {
     public GameObject mainPanel;
     public GameObject optionsPanel;
 
     private Outline outline;
 
     void Awake()
     {
         mainPanel.SetActive(true);
         optionsPanel.SetActive(false);
     }
 
     public void exitGame()
     {
         #if UNITY_EDITOR
             UnityEditor.EditorApplication.isPlaying = false;
         #else
             Application.Quit();
         #endif
     }
 
     public void showOptions()
     {
         mainPanel.SetActive(false);
         optionsPanel.SetActive(true);
     }
 
     public void showMain()
     {
         mainPanel.SetActive(true);
         optionsPanel.SetActive(false);
     }
     public void inactiveOutlines()
     {
         outline = mainPanel.GetComponentInChildren<Outline>();
 
         Debug.Log(outline.IsActive());
         Debug.Log(outline);
     }
 }
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 GilbertoBitt2 · Nov 22, 2016 at 02:31 PM 0
Share
     Outline outline;

     outline.enabled = false;

3 Replies

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

Answer by lazerblade01 · Nov 23, 2016 at 07:32 AM

OK, so after getting some sleep, seeing GilbertoBitt2's answer, and doing a little more playing around, I came up with the following:

     private Outline[] outline;
     private Outline[] outline2;
 
     public void inactiveOutlines()
     {
         outline = mainPanel.GetComponentsInChildren<Outline>();
         foreach(Outline o in outline)
         {
             o.enabled = false;
             Debug.Log(o.name);
         }
         outline2 = optionsPanel.GetComponentsInChildren<Outline>();
         foreach (Outline o in outline2)
         {
             o.enabled = false;
             Debug.Log(o.name);
         }
     }
 

I just call my function onClick() of each button, and it takes care of its siblings. Hopefully this helps others who get stuck.

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
avatar image
1

Answer by GilbertoBitt2 · Nov 22, 2016 at 02:38 PM

     Outline outline;

     outline.enabled = false;
Comment
Add comment · Show 1 · 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 lazerblade01 · Nov 22, 2016 at 05:21 PM 0
Share

Where would this go? After adding in the enabled = false, only 1 of the "buttons" is removing the outline. It would seem that the "Options" screen is properly reverting the outline to false, but the main menu is not. Does calling Outline outline; get all outlines in the currently active panel / group?

avatar image
1

Answer by elenzil · Nov 23, 2016 at 12:02 AM

Gilberto is calling out the difference between GameObjects and Components.

A GameObject can have many components. Transform, Image, Button, Outline, etc.

To turn an entire GameObject on & off, use MyGameObject.SetActive(). To turn just one component on & off, use MyComponent.enabled.

As far as turning off the outline on all the buttons, that's sort of up to you. You might want to look into GetComponentsInChildren<>(). note plural "Components".

Comment
Add comment · Show 1 · 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 lazerblade01 · Nov 23, 2016 at 12:13 AM 1
Share

That's basically what I ended up doing, then iterating over each one. I even posted my own code that got it working, but for some reason it was removed. Not sure why. I guess answering my own question through multiple iterations until success is found is not how things work here? No idea.

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

How to click and drag images into multiple slots? 0 Answers

Unity 2D Combat Text Issue With Multiple Enemies 2 Answers

Moving a ScrollRect child via script to focus on some point. 0 Answers

Editing UIElements Label through code and alignment 1 Answer

Score counter breaking after adding points 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