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 mmyoonsin94 · Mar 21, 2018 at 08:31 AM · setactiveonclickswitch-casestudent

How to apply switch case on the button to activate and deactivate game object?

  //Collect the game object
  public GameObject[] sorting;

  //Collect the button
  public GameObject[] sortingBtn;
  int j;
  int b;
 
 void Start () {
         sorting = GameObject.FindGameObjectsWithTag("Sort");
         sortingBtn = GameObject.FindGameObjectsWithTag("SortButton");
 
         for (j = 0; j < sorting.Length; j++)
         {
             Debug.Log(j + " " + sorting[j].name);
         }
         for (b = 0; b < sortingBtn.Length; b++)
         {
             Debug.Log(b + " " + sortingBtn[b].name);
         }
         Invoke("CurrentScene", 1);
 }
 
     public void CurrentScene()
     {
         switch (b)
         {
             case 1:
                 Debug.Log("Plastic hellooo");
                 sorting[1].SetActive(true);
                 sorting[0].SetActive(false);
                 break;
             case 0:
                 Debug.Log("Paper hellooo");
                 sorting[0].SetActive(true);
                 sorting[1].SetActive(false);
                 break;
             default:
                 Debug.Log("Default hellooo");
                 sorting[0].SetActive(true);
                 sorting[1].SetActive(false);
                 break;
         }
     }

The case default works well at the beginning. But when click the UI button to switch the case it doesn't work. How to make the button respond to the condition that has be set in switch case statement? (The button had been attached with pubic void CurrentScene in Unity).

Comment
Add comment · Show 3
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 kuijerlattie · Mar 21, 2018 at 10:33 AM 1
Share

the code you have right now seems to just count the total amount of buttons and change visibility based on that number. if you want to change things based on the clicked button you need to get references to the button script and add a function to the onclick. Is that what you want to achieve?

avatar image mmyoonsin94 kuijerlattie · Mar 21, 2018 at 11:02 AM 0
Share

Yes, there is the thing I want to achieve. So what you means here is each of the buttons will refer to different function? Seem like the switch case won't be work well in this case.

Sorry for my bad English. :)

avatar image kuijerlattie mmyoonsin94 · Mar 21, 2018 at 11:03 AM 0
Share

Let me write a quick answer that should work. expect a reply in a few $$anonymous$$utes

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by kuijerlattie · Mar 21, 2018 at 11:08 AM

   //Collect the game object
   public GameObject[] sorting;
   //Collect the button
   public GameObject[] sortingBtn;
   int j;
   int b;
  
  void Start () {
          sorting = GameObject.FindGameObjectsWithTag("Sort");
          sortingBtn = GameObject.FindGameObjectsWithTag("SortButton");
  
          for (j = 0; j < sorting.Length; j++)
          {
              Debug.Log(j + " " + sorting[j].name);
          }
          for (b = 0; b < sortingBtn.Length; b++)
          {
              Debug.Log(b + " " + sortingBtn[b].name);
              Button btn = sortingBtn[b].GetComponent<Button>();
              btn.onClick.AddListener(() => Currentscene(b));
          }
          Invoke("CurrentScene", 1);
  }
  
      public void CurrentScene(int buttonnumber)
      {
          switch (buttonnumber)
          {
              case 1:
                  Debug.Log("Plastic hellooo");
                  sorting[1].SetActive(true);
                  sorting[0].SetActive(false);
                  break;
              case 0:
                  Debug.Log("Paper hellooo");
                  sorting[0].SetActive(true);
                  sorting[1].SetActive(false);
                  break;
              default:
                  Debug.Log("Default hellooo");
                  sorting[0].SetActive(true);
                  sorting[1].SetActive(false);
                  break;
          }
      }


this should do the trick. if button is not available, put: using UnityEngine.UI; at the top of the file.

Comment
Add comment · Show 11 · 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 kuijerlattie · Mar 21, 2018 at 11:10 AM 0
Share

You can even remove the
Invoke("CurrentScene", 1);
and replace it with:
CurrentScene(-1);
to trigger the default case of the switch.

avatar image kuijerlattie · Mar 21, 2018 at 12:04 PM 0
Share

Did this work?

avatar image mmyoonsin94 kuijerlattie · Mar 21, 2018 at 12:33 PM 0
Share

$$anonymous$$ay be I need to add up something?

avatar image mmyoonsin94 kuijerlattie · Mar 22, 2018 at 05:40 AM 0
Share

alt text

Something miss out there, set the parameter to each button, as shown as figure above. And I delete the 2 rows code of button onclick. Then, it works well for me now. I think I should do some works in part of scripting to set value to the button ins$$anonymous$$d of just set the parameter of button in Unity even though it's a quite convenient way. :)

k.jpg (14.6 kB)
avatar image kuijerlattie mmyoonsin94 · Mar 22, 2018 at 08:37 AM 0
Share

the image doesnt show, can you upload it to imgur or some other hosting site and link it here?

Show more comments
Show more comments
avatar image mmyoonsin94 · Mar 21, 2018 at 12:22 PM 0
Share

I'm trying it out. $$anonymous$$ay I ask how the -1 is actually works to trigger the function CurrentScene()? So far I have changed the code you suggest, the game start up like what I had set in case default, and when I click one of the buttons, nothing change. From the console, I can see that Debug.Log: Default hellooo and Paper hellooo come out at the same time when I click any one the buttons but the Plastic hellooo not even show out. Thank you for your help. :)

avatar image kuijerlattie mmyoonsin94 · Mar 21, 2018 at 12:56 PM 0
Share

the -1 version is only for the initial trigger when the script gets loaded. the onclick handlers give the version with the number linked to the button. every button has their own value that they pass trough (their position in the list). if you want to increase it every time a button gets clicked you have to make a seperate counter and add to it everytime the function gets called

avatar image kuijerlattie mmyoonsin94 · Mar 21, 2018 at 12:58 PM 0
Share

also: within CurrentScene(-1) the -1 part is just the parameter that gets passed to the CurrentScene function, not the function itself. when you call Invoke it just waits X seconds before executing wouthout a value.

avatar image
1

Answer by upasnavig90 · Mar 22, 2018 at 11:30 AM

I guess u need to send the butonNumber parameter on buttonClick, on the basis of which switch case will work.alt text


capture.png (18.9 kB)
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

77 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

Related Questions

physics.OverlapSphere colliders 1 Answer

Should I use Destroy() or SetActive(false)? 1 Answer

GameObject not showing up when set active. 1 Answer

Setting 'SetActive' to TRUE not working 5 Answers

.SetActive (false) deletes gameObject but it's effects still affect the player 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