Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 imagoculturae · Jul 19, 2015 at 07:23 PM · uitoggle

TOGGLE UI problem

Really don't get this: I have 2 UI objects, eachone having its one toggle, from the toggles I call 2 functions from the same script. The first function is the same for both GameObjects, which is an animation.Then I have 2 more function in order to choose which object should animate:

     private Animator anim;
     public GameObject target;
     public GameObject panelCamera;
     public GameObject panelEnvironment;
     
 
     void Start () {
   
         anim = target.GetComponent<Animator>();
         
         
     }
     
     .....
 
     public void chooseCamera(){
          
         target = panelCamera;
 
     }
     public void chooseEnvironment(){
 
         target = panelEnvironment; 
 
     }


Now why if I put anim = panelCamera.GetComponent(); or anim = panelEnvironment.GetComponent(); it works for each chosen one and if I use anim = target.GetComponent(); it always uses panelCamera although in the editor I can see the switch?

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

3 Replies

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

Answer by KdRWaylander · Jul 20, 2015 at 08:00 AM

Hi,

That's because you define anim in the Start function, Start function will not be called again so it will not reassign when target change. Unless you directly write it down in Update of any repeatable function, variables are not dynamically changed. When you set up a value it keeps that value while you don't change it "manually". I guess you drag and dropped panelCamera in the editor.

What you need to do: in chooseCamera() and chooseEnvironment() you need to add another taget.GetComponent() line:

 public void chooseCamera () {
    target = panelCamera;
    anim = target.GetComponent<Animator>();
 }
 
 public void chooseEnvironment () {
    target = panelEnvironment;
    anim = target.GetComponent<Animator>();
 }
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
0

Answer by imagoculturae · Jul 20, 2015 at 09:20 AM

Hi Thanks a lot, this works! However I wanted to experiment different things and I end up doing-it like this:

 public GameObject panelCamera;
 public GameObject panelEnvironment;
 public Animator[] alls;

....

     public void chooseCamera(){
         foreach(Animator all in alls){
             all.GetComponent<Animator>().enabled = true;
             all.GetComponent<Animator>().Play("PanelSlideOut");
         }
         panelCamera.GetComponent<Animator>().Play("PanelSlideIn");
 
     }
     public void chooseEnvironment(){
         foreach(Animator all in alls){
             all.GetComponent<Animator>().enabled = true;
             all.GetComponent<Animator>().Play("PanelSlideOut");
         }
         panelEnvironment.GetComponent<Animator>().Play("PanelSlideIn");
     }

 

It works fine but when I click for the first time I get a strange behavior, there must be something wrong with my animator, you can check a test here

p.s. press F1 for controls

Comment
Add comment · Show 2 · 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 KdRWaylander · Jul 20, 2015 at 11:50 AM 0
Share

Is your green panel under your other panels in the hierarchy ? If this is the case, try putting the brown panel under the others and run your game: do you have the same strange behaviour but with brown panel this time ?

avatar image imagoculturae · Jul 21, 2015 at 07:54 AM 0
Share

Yes I do get the same behavior with the brown panel when I put it at the bottom, what should I do?

avatar image
0

Answer by KdRWaylander · Jul 21, 2015 at 12:03 PM

Well first, before to play your SlideIn animation you should set the coloured panel you want to rise as last or first sibling. Because right now you don't have the same behaviour between every cases: green appears atop of red while red appears from behind green so you begin to see it at the middle of your animation (while you see green for all its animation). Switch between red and green multiple times and observe how they are different. If that's an effect you wanna make, you can keep it ofc ^^

About the initialization: that's not a problem, that's just that you ask the "SlideOut" animation for all your panels before to "SlideIn" one panel (and green is atop all the others that's why you only see this one but they are all moving pilled up, and you only saw the brownish one when you changed the order, because the lower in the hierarchy, the upper in the displayed layer). But since your animation (the animation file) starts with all panels up on first key and then puts them down, when you play the animation it sets the panel in the good state (which is up) and then plays.

So that was the cause, now, solution:

In your button functions you need to know if it's the first time you slide the panels or not. Let's use a boolean for that. This boolean must be declared for the whole script so you can click on any button and it will works anyway.

 bool firstSlide = true;

Now, in every button function you need to do this change: IF this is the first time i need to slide a panel: don't play the slide out animation ELSE you can play slide out.

 public void chooseXXX () {
    if (firstSlide) {
       firstSlide = false;
       panelXXX.GetComponent<Animator>().Play("PanelSlideIn");
    } else {
       // stuff you already wrote and that works :)
       foreach () {
          // ...
       }
       panelXXX. //...
    }
 } 


I hope it was clear enough for you ^^

Comment
Add comment · Show 6 · 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 imagoculturae · Jul 21, 2015 at 12:57 PM 0
Share

Hi kdr thanks a lot for your help...however I get the same result when I click for the first time you can check here

 public GameObject panelObjects;
         public GameObject panelCamera;
         public GameObject panelEnvironment;
         public Animator[] alls;
     //...
         bool firstSlide = true;
     
     //...
     public void chooseObjects(){
     
             if (firstSlide) {
     
                 firstSlide = false;
                 panelObjects.GetComponent<Animator>().Play("PanelSlideIn");
     
             } else {
                 foreach(Animator all in alls){
                     all.GetComponent<Animator>().enabled = true;
                     all.GetComponent<Animator>().Play("PanelSlideOut");
                 }
                 panelObjects.GetComponent<Animator>().Play("PanelSlideIn");
             }
         }
     
     
         public void chooseCamera(){
             if (firstSlide) {
                 firstSlide = false;
                 panelCamera.GetComponent<Animator>().Play("PanelSlideIn");    
             } else {
                 foreach(Animator all in alls){
                 all.GetComponent<Animator>().enabled = true;
                 all.GetComponent<Animator>().Play("PanelSlideOut");
                 }
             panelCamera.GetComponent<Animator>().Play("PanelSlideIn");
             }
         }
     
     
         public void chooseEnvironment(){
             if (firstSlide) {
                 firstSlide = false;
                 panelEnvironment.GetComponent<Animator>().Play("PanelSlideIn");    
             } else {
                 foreach(Animator all in alls){
                 all.GetComponent<Animator>().enabled = true;
                 all.GetComponent<Animator>().Play("PanelSlideOut");
                 }
             panelEnvironment.GetComponent<Animator>().Play("PanelSlideIn");
             }
         }
 

    



avatar image KdRWaylander · Jul 21, 2015 at 01:38 PM 0
Share

How are you doing your animations ? $$anonymous$$odifying the size of the rect ? Are your Animators component all disabled at the beginning ? Try setting the Animator to enabled = true when it's the first slide.

 if (firstSlide) {
    firstSlide = false;
    panelXXX.GetComponent().enabled = true;
    panelXXX.GetComponent().Play("PanelSlideIn");
 }

avatar image imagoculturae · Jul 21, 2015 at 02:23 PM 0
Share

Almost there but still not perfect(the second time I click shows an other panel)...I am translating the panels downward, I set the animator to false on F1 because I did not want to sldein, this is my full script and the updated file

 public class $$anonymous$$ySlideScript2 : $$anonymous$$onoBehaviour {
 
 
     public GameObject panelObjects;
     public GameObject panelCamera;
     public GameObject panelEnvironment;
     public Animator[] alls;
     public GameObject canvas;
     private bool settings = false;
     private bool firstSlide = true;
 
     void Start () {
 
         foreach(Animator all in alls){
             all.GetComponent<Animator>().enabled = true;
         }
 
         canvas.SetActive(false);
     }
 
     public void Update () {
 
 
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F1) && !settings){
 
             foreach(Animator all in alls){
                 all.GetComponent<Animator>().enabled = false;
             }
             canvas.SetActive(true);
             settings = true;
                 
         }
                 
         else if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F1) && settings){
             foreach(Animator all in alls){
                 all.GetComponent<Animator>().enabled = true;
             }
             canvas.SetActive(false);
             settings = false;
         }
     }
 
     public void chooseObjects(){
         if (firstSlide) {
             firstSlide = false;
             panelObjects.GetComponent<Animator>().enabled = true;
             panelObjects.GetComponent<Animator>().Play("PanelSlideIn");
         } else {
             foreach(Animator all in alls){
                 all.GetComponent<Animator>().enabled = true;
                 all.GetComponent<Animator>().Play("PanelSlideOut");
             }
             panelObjects.GetComponent<Animator>().Play("PanelSlideIn");
         }
     }
 
 
     public void chooseCamera(){
         if (firstSlide) {
             firstSlide = false;
             panelCamera.GetComponent<Animator>().enabled = true;
             panelCamera.GetComponent<Animator>().Play("PanelSlideIn");    
         } else {
             foreach(Animator all in alls){
             all.GetComponent<Animator>().enabled = true;
             all.GetComponent<Animator>().Play("PanelSlideOut");
             }
         panelCamera.GetComponent<Animator>().Play("PanelSlideIn");
         }
     }
 
 
     public void chooseEnvironment(){
         if (firstSlide) {
             firstSlide = false;
             panelEnvironment.GetComponent<Animator>().enabled = true;
             panelEnvironment.GetComponent<Animator>().Play("PanelSlideIn");    
         } else {
             foreach(Animator all in alls){
             all.GetComponent<Animator>().enabled = true;
             all.GetComponent<Animator>().Play("PanelSlideOut");
             }
         panelEnvironment.GetComponent<Animator>().Play("PanelSlideIn");
         }
     }


avatar image KdRWaylander · Jul 21, 2015 at 02:40 PM 0
Share

Alright, got it i think :)

In each function, between .enabled and .Play(), add this line:

 panelXXX.transform.SetAsLastSibling();

And upload me the result :)

avatar image imagoculturae · Jul 21, 2015 at 07:00 PM 0
Share

Did that but on the second click if I choose another panel they all Slide out,after the second click it is fine, here is the new file

Show more comments

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

22 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

Related Questions

On Value changed event fired without the value changed. 1 Answer

Changing multiple toggle states. 0 Answers

Mute toggle nor working on Android?? 0 Answers

Do I have to resize and manually move the checkbox with a Unity Toggle? 0 Answers

Toggle's 'On Value Changed' checkbox is shown for bool parameter function 0 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