Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 AshwinTheGammer · Nov 07, 2020 at 12:38 PM · uiguibutton trigger eventsevent triggeringevent-listener

How to detect if UI button is clicked?

Yeah, I know this question has been asked thousand times on the internet... But I find it very confusing...Because I'm just a beginner...

I'm converting GUI Button Eventto UI Button Event

Here's my code:

  public void ShowSave() 
     {
         for (int i = -1; i < saveGames.Count; i++)
         {
 
             if (i == selectedSaveGameIndex)
             {
 
 
                 string str = SaveInputField.GetComponent<InputField>().text = saveGameName;
                 //string str = GUILayout.TextField(saveGameName, GUILayout.MinWidth(200));
 
                 if (regularExpression.IsMatch(str))
                 {
                     if (str.IndexOfAny(newLine) != -1)
                     {
                         //New Line detected
                         if (i >= 0)
                         {
                             SaveLoad.DeleteFile(slu.saveGamePath, saveGames[i].savegameName);
                         }
                         slu.SaveGame(saveGameName);
                         selectedSaveGameIndex = -99;
                         return;
                     }
                     else
                     {
                         saveGameName = str; //All OK, copy
                     }
                 }
                 else
                 {
                     Debug.Log("Irregular expression detected");
                 }
  //This LINE
                 if (GUILayout.Button("Save", GUILayout.MaxWidth(50)))
                 {
                     if (i >= 0)
                     {
                         SaveLoad.DeleteFile(slu.saveGamePath, saveGames[i].savegameName);
                     }
                     slu.SaveGame(saveGameName);
                     selectedSaveGameIndex = -99;
                     saveGames = SaveLoad.GetSaveGames(slu.saveGamePath, slu.usePersistentDataPath);
                     return;
                 } //THIS LINE
 


I want this to happen when the user Click on the button : if (GUILayout.Button("Save", GUILayout.MaxWidth(50))) { if (i >= 0) { SaveLoad.DeleteFile(slu.saveGamePath, saveGames[i].savegameName); } slu.SaveGame(saveGameName); selectedSaveGameIndex = -99; saveGames = SaveLoad.GetSaveGames(slu.saveGamePath, slu.usePersistentDataPath); return;

Thanks!

Comment
Add comment · Show 4
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 Strawbot · Nov 07, 2020 at 12:43 PM 0
Share

Well, normally, the way you wrote it works perfectly. Doesn't it?

avatar image AshwinTheGammer Strawbot · Nov 07, 2020 at 01:54 PM 0
Share

Well it does... But the problem is, it's written for GUI based interface I wanna convert it into UI based interface

avatar image eses · Nov 07, 2020 at 06:04 PM 0
Share

Is this for game/runtime? I'm asking because you are using old I$$anonymous$$GUI, which should mostly be only used for Editor scripting...

avatar image AshwinTheGammer eses · Nov 08, 2020 at 07:32 AM 0
Share

game .......

3 Replies

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

Answer by prakyathd801 · Nov 12, 2020 at 12:46 PM

 public int ShowSave() 
      {
          for (int i = -1; i < saveGames.Count; i++)
          {
  
              if (i == selectedSaveGameIndex)
              {
                  string str = SaveInputField.GetComponent<InputField>().text = saveGameName;
                  //string str = GUILayout.TextField(saveGameName, GUILayout.MinWidth(200));
  
                  if (regularExpression.IsMatch(str))
                  {
                      if (str.IndexOfAny(newLine) != -1)
                      {
                          //New Line detected
                          if (i >= 0)
                          {
                              SaveLoad.DeleteFile(slu.saveGamePath, saveGames[i].savegameName);
                          }
                          slu.SaveGame(saveGameName);
                          selectedSaveGameIndex = -99;
                          return;
                      }
                      else
                      {
                          saveGameName = str; //All OK, copy
                      }
                  }
                  else
                  {
                      Debug.Log("Irregular expression detected");
                  }
 return i;
 }
   //This LINE
 //Call this function on onclicking the button
 public void newfunction()
 {
 int i= ShowSave() ;
                  if (GUILayout.Button("Save", GUILayout.MaxWidth(50)))
                  {
                      if (i >= 0)
                      {
                          SaveLoad.DeleteFile(slu.saveGamePath, saveGames[i].savegameName);
                      }
                      slu.SaveGame(saveGameName);
                      selectedSaveGameIndex = -99;
                      saveGames = SaveLoad.GetSaveGames(slu.saveGamePath, slu.usePersistentDataPath);
                      return;
                }  } //THIS LINE
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 AshwinTheGammer · Nov 13, 2020 at 03:09 AM 0
Share

thnx bro! it worked! I edited the script further more!

avatar image
0

Answer by thevortexbm · Nov 07, 2020 at 01:15 PM

Create a public method and assign it to the On Click method in the Editor. Make sure the script is on a GameObject

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 AshwinTheGammer · Nov 07, 2020 at 01:51 PM 0
Share

The problem is "i" is local variable. How can I declare it beyond the scope? That's the problem. I think you didn't read the script fully.

avatar image
0

Answer by sevalk · Nov 07, 2020 at 03:06 PM

Create a public method that contains the actions you want to be done when the button is clicked. Create a GameObject in unity and assign the script containing this method to this GameObject. Then assign this GameObject to the Onclick method of the button you created and select the public method you want it to work with.

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 AshwinTheGammer · Nov 08, 2020 at 12:29 AM 0
Share

i is declared in showsave(), o which it's local var... How can I declare it another method. I know what are you saying, and I know that. Read the method again. Thnx!

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

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

Hold-able UIButtons inside a Scroll Rect 0 Answers

How do I asign this script to a button successfully? 1 Answer

Button AddListener during runtime pointing to another GameObject? 1 Answer

Function in the Event Trigger of a UI element does not work properly 0 Answers

How do I allow my UI buttons to detect my "GameMaster" object after returning to the scene for OnClick events? 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