Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
1
Question by Minarstilis · Nov 26, 2016 at 04:21 AM · eventdelegate

Button.onClick how to pass parameter?

A button in class UIManager, when the button click, how to pass params(such as scene name) to the method LoadLevelByName in class LevelManager .

 public class UIManager : MonoBehaviour {
 
     [SerializeField]
     private Button _startButton;
 void Start ()
     {
         _startButton.onClick.AddListener(LevelManager.Instance.GotoLevel);
     }
 }

 public class LevelManager : MonoBehaviour
 {
 
     public static LevelManager Instance;
     public UnityAction GotoLevel;
 
     void Awake ()
     {
         if ( Instance == null )
         {
             Instance = this;
             DontDestroyOnLoad(this.gameObject);
         }
         else if ( Instance != null )
         {
             Destroy(this.gameObject);
         }
     }
 
     void Start ()
     {
         GotoLevel = () => LoadLevelByName();
     }
     public void LoadLevelByName (string levelName)
     {
         SceneManager.LoadScene(levelName);
     }
 }



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 dacarrera · Nov 26, 2016 at 08:30 AM 3
Share

You have to decide when you want this function to call, and then you'll pass in a parameter at that time. If you want to call it when the script is executed, you can just call LoadLevelByName(levelName); and it'll run. If you want to do it when a button is clicked, you can add the script to a button's onClick(); and pass in the scene name you want to load when it's clicked.

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by VRAndAR · Jan 22, 2018 at 07:24 AM

If your Method has only one argument, Then the argument can be passed as a parameter in the inspector itself.If it has more than one parameter then you should register to events such as the addListener Event in unity when the button is clicked.

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 junedmmn · Nov 10, 2018 at 05:32 PM 2
Share

@VRAndAR, you are supposed to give solution, rather than just giving suggestions without any description or procedure.

avatar image LoloMiMama junedmmn · Aug 10, 2020 at 06:09 PM 5
Share

He's not supposed to give a solution of any kind. If you expect everything co$$anonymous$$g from above, you'd better find another profession and stop developing games.

He took time to give you a hand and you reply that with arrogance. Shame on you.

avatar image
2

Answer by Theinsanekiller · Sep 26, 2019 at 03:17 AM

Check here: https://answers.unity.com/questions/1288510/buttononclickaddlistener-how-to-pass-parameter-or.html

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

85 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

Related Questions

I have created an Event based InputManager 0 Answers

Event NullReferenceException 1 Answer

Delegates & Events registration & deregistration - Doubt 0 Answers

Question regarding delegates and events 1 Answer

Delegate.CreateDelegate fail to call correct overloaded 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