Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using return values from Methods as argument for Events

Discussion in 'UGUI & TextMesh Pro' started by Legi, Aug 22, 2014.

  1. Legi

    Legi

    Joined:
    Jul 25, 2013
    Posts:
    20
    Hi,

    Is it possible to have events accept return values from methods as an parameter rather than having to "hardcode" a string for example?
    I could pass a script so the method the event calls gets the desired value itself.

    But that's not very practical :/

    I would like to pass a variable or a method with a return type.

    Making it a bit more dynamic without having to write scripts to have this behaviour :)

    I would try to extending the event system or something like that if that's possible. But maybe i was just missing something
     
    rakkarage likes this.
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,221
    Hi, I don't quite understand the question. Would you be able to show me some code that shows how you would like this to work so I could understand?
     
  3. santi8ago8

    santi8ago8

    Joined:
    May 15, 2014
    Posts:
    1
    I think I have the same problem.
    I have this function

    Code (CSharp):
    1. public void Click (Object o)
    with the editor I add it as follows:

    But.... If I want to do for code
    Code (CSharp):
    1. btn.GetComponentInChildren<UnityEngine.UI.Button> ().onClick.AddListener (Click);
    Gives me an error....
    The only solution I found was removing the parameter of the function, but I need the parameter.

    So, can you help me do this with code?
     
  4. Legi

    Legi

    Joined:
    Jul 25, 2013
    Posts:
    20
    Sure :)

    Suppose i have a class like this:
    Code (CSharp):
    1. public class RandomStringGenerator : MonoBehaviour {
    2.     public string GetRandom() {
    3.         return // string with random characters;
    4.     }
    5. }
    When i click on a button i want to change the text of a Text Component to the value "GetRandom" returns.
    Right now i would have to create a new script which is attached to that button:
    Code (CSharp):
    1. public class ChangeText : MonoBehaviour, IPointerClickHandler {
    2.     public RandomStringGenerator rsg;
    3.     public Text label;
    4.  
    5.     public void OnPointerClick(PointerEventData eventData) {
    6.         label.text = rsg.GetRandom();
    7.     }
    8. }
    That way i get the desired funcionality, but it's strange that i have to create a script just to assign a value that could change.

    It would be great to be able to set something like this up right in the button component.

    I hope i got it this time :D
     
    Last edited: Aug 24, 2014
    rakkarage likes this.
  5. Legi

    Legi

    Joined:
    Jul 25, 2013
    Posts:
    20
    Any news on that Front? :)
     
  6. ScriptKid

    ScriptKid

    Joined:
    Aug 8, 2014
    Posts:
    23
    i know this kind of stuff from c++ since your return value is a normal type it should be possible to take this return value and give it as agument to another function like
    Code (CSharp):
    1. function1(function2());
    or u call function1 inside of function2 like
    Code (CSharp):
    1. function2(){
    2. random=function1();
    3. }
     
  7. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,221
    I understand your use case, but it's not something that is easy to support or expose. Take for example you have a hook up with multiple targets that all return something, which one of the targets sets the value on the text?
     
    rakkarage likes this.
  8. sdpgames

    sdpgames

    Joined:
    Sep 2, 2014
    Posts:
    17