Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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
3
Question by Obsdark · Nov 28, 2016 at 09:08 PM · c#scripting problemfunctionsparametersmethods

How can i pass a method like a parameter Unity3d C#?

i'm trying to pass a function to another function as a parameter, how make this work?

This is an example of what i need to do:

 CreateButton( new BuildModeController().BuildObject_BuildInstalledObject("Wall") );


How i must write the method?

 void CreateButton( [What i need to write here?] )
 {
      . . . do something.
 }


Have in consideration than BuildObject_BuildInstalledObject("Wall") doesn't return anything (yes, is a void method), an itself requeire a parameter, this method actualy make something in the worldmap

this is the actual CreateButton method:

 void CreateButton(Canvas panel, Vector2 newVector2Position, Vector2 newVector2size, Action<string> method, Sprite buttonSprite, string stringNameButton)
         {
             DicButtonSprite.TryGetValue ("TileBlancoTibia", out ButtonSprite);
     
             //Create button & Text
             Button button = (new GameObject ("Button", typeof(CanvasRenderer), typeof(Image), typeof(Button))).GetComponent<Button> ();
             Text txt = (new GameObject ("Texto", typeof(CanvasRenderer), typeof(Text))).GetComponent<Text> ();
     
             //Installing Hierarchys
             button.transform.parent = panel.transform;
             txt.transform.parent = button.transform;
     
             //Posición y tamaño
             button.transform.position = newVector2Position;
             button.GetComponent<RectTransform> ().sizeDelta = newVector2size;
             txt.transform.position = newVector2Position;
             txt.GetComponent<RectTransform> ().sizeDelta = newVector2size;
     
             //txt preparations
             txt.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
             txt.alignment = TextAnchor.MiddleCenter;
             txt.text = stringNameButton;
             txt.color = Color.black;
             txt.fontSize = 12;
     
             //Imagen
             button.GetComponent<Image>().sprite = buttonSprite;
     
             //Metodo al que llama
             button.GetComponent<Button>().onClick.AddListener(method);
             button.GetComponent<Text>().text = stringNameButton;
         }

of course, this method doesn't work, and i'm looking to change the Action<string> method part

i'm trying to archive this to add to a button, through an addListener, if you have that in consideration would be excelent.

Thanks in advance

Comment
Add comment · Show 2
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 callme · Nov 28, 2016 at 09:15 PM 0
Share

like this?

  1. function objc(){ return transform } - function parameter

  2. function create(Transform t){}

avatar image Obsdark callme · Nov 28, 2016 at 09:31 PM 0
Share

I'm not in use or need of pass a transform, but i need to pass a function in order to add to the object through an addlistener, any ideas in how to archieve this? thanks in advance

3 Replies

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

Answer by ThePersister · Nov 29, 2016 at 12:58 PM

Here's an example of how you would pass a method along as a parameter.

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour 
 {
     public delegate void TestDelegate(); // This defines what type of method you're going to call.
     public TestDelegate m_methodToCall; // This is the variable holding the method you're going to call.
 
     void Start() 
     {
         // Fill Delegate.
         m_methodToCall = AwesomeExampleMethod; // Notice we don't use (); here. (that can be confusing)
 
         // Call method, pass along delegate.
         SimpleMethod( m_methodToCall );
     }
    
     // This method expects a TestDelegate variable, allowing us to pass a custom void method.
     private void SimpleMethod(TestDelegate method)
     {
         Debug.Log( "I'm about to call a method" );
         method();
     }
 
     // A random method that is compatible with TestDelegate (asks no parameters, returns void)
     private void AwesomeExampleMethod() 
     {
         Debug.Log( "Yay!" );
     }
 }
 

I hope that helps, if it did, please accept my answer. If you need any details, let me know!

Best of luck!

Cheers,

ThePersister

Comment
Add comment · Show 3 · 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 ThePersister · Nov 29, 2016 at 01:05 PM 1
Share

Extra details about the string parameter setup you want:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Example : $$anonymous$$onoBehaviour 
 {
     public Button button;
 
     public delegate void TestDelegate(string test); // This defines what type of method you're going to call.
     public TestDelegate m_methodToCall; // This is the variable holding the method you're going to call.
 
     void Start() 
     {
         // Fill Delegate.
         m_methodToCall = new Build$$anonymous$$odeController().BuildObject_BuildInstalledObject; // Notice we don't use (); here. (that can be confusing)
 
         // Call method, pass along delegate.
         Simple$$anonymous$$ethod( m_methodToCall );
     }
    
     // This method expects a TestDelegate variable, allowing us to pass a custom void method.
     private void Simple$$anonymous$$ethod(TestDelegate method)
     {
         Debug.Log( "I'm about to call a method" );
         button.onClick.AddListener( () => method("Wall") ); // Note how we use the "Wall" bit here.
     }
 
     // A random method that is compatible with TestDelegate (asks no parameters, returns void)
     private void AwesomeExample$$anonymous$$ethod(string test) 
     {
         Debug.Log( "Yay!" );
     }
 }
 
avatar image Obsdark · Nov 29, 2016 at 07:14 PM 1
Share

This is strange, it works, i mean i can use the method, the Debug.Log()'s emited when i press the button are telling me that it work, so thanks for helping me to learn delegates, now the thing is even when this method "Works" (i.e. pass the code through the stored method) it doesn't make the effect than i'm looking for -and suppose to do-, thanks in any case, i'm gonna gave you the answered question because, i think, this problem is for a new question, Thanks a lot!.

avatar image ThePersister Obsdark · Nov 30, 2016 at 09:16 AM 0
Share

You're welcome, I'm glad I could help, best of luck with the future issues! :)

avatar image
0

Answer by SeanKilleen · Nov 28, 2016 at 09:39 PM

You can use delegates in c# to pass functions as an argument to other functions.

https://msdn.microsoft.com/en-us/library/ms173171.aspx

 public delegate int PerformCalculation(int x, int y);
 void CreateButton(int x)
 
 CreateButton(PerformCalculation(x,y));

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 AkarshJain · Jul 03, 2021 at 09:40 AM

for me the ans was

 public Button button;
 
 public void Func(UnityEngine.Events.UnityAction Func2){
 
      // i wanted to use it to change button on click listener
 
     button.onClick.Addlistner(Func2);
 
 }




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

273 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

Related Questions

How do I call a method with variables in it? 1 Answer

Help with an error in scripting - extreme newbie 3 Answers

How can I change a function from another script? 1 Answer

[C#]How to save a list of items to use in another script and it's not a player prefs type of list? 0 Answers

How to automatically generate a new int with a specific name? 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