- Home /
How do I type my own paramater in a function on an input field?
This function takes a string as a parameter, but since it's on an input box, it's taking the text of the input field as the parameter. How do I make it so I can type in my own string instead?
(this is for unity 2017)
Answer by Bunny83 · Jan 24, 2020 at 05:01 PM
Well, It's not clear what kind of UnityEvent this is. If it is a UnityEvent that actually takes a string parameter when invoked, that parameter is passed from the caller. If the field is just an ordinary UnityEvent without parameters, you have to select the right method from the drop down list. It might be listed several times, once without parameter and once with.
the "unity event" is the "vrc_trigger.executecustomtrigger"? then yes it takes a string as a parameter.
the "caller" is the inputfield? well i want to know how to write in my own string as a parameter, ins$$anonymous$$d of using the one from the inputfield. if i put this function on a button, then it does let me type in my own.
No, you did not understand what I meant ^^. I was talking about the script which has this UnityEvent field where you want to assign your VRC_Trigger.ExecuteCustomTrigger method to.
We have no context where you actually assigned your method to. What is the type of field of this UnityEvent? Do you have written the script where the inspector in the screenshot belongs to? If so, that code is what I was asking for.
For example: If you write this in a script:
public UnityEvent someEvent;
Then when you inspect your script you will see exactly this inspector snippet that you have shown us. However there are different UnityEvent types available. This one has no parameter at all. Within this script we can just use: someEvent.Invoke();
to actually call the method whatever you assigned in the inspector.
Now there are also other UnityEvents and this one takes one parameter. If order to use them you have to create a derived class with your desired parameter type as shown in the example in the documentation. When we use that new type $$anonymous$$yIntEvent
in a script we again get the same inspector we see in your screenshot:
pubic $$anonymous$$yIntEvent someOtherEvent;
However this time this event requires an int parameter. Now the user (who assigns a method in the inspector) can only select methods which actually take an int parameter. This int parameter has to be passed when you invoke / call that event
someOtherEvent.Invoke(42);
When you assign a method with a parameter to a UnityEvents without parameters, the inspector will allow you to specify one parameter if the parameter is a primitive type (string, int, float, bool, ....).
If the event is just a parameterless event you can assign methods without parameters (of course) and also methods with one parameter.
If the event type already expects a string parameter you can select different options. The method listing is divided into "dynamic string" methods at the top and "static parameters" on the bottom. A method with a string parameter will show up in both sections. However the top one will receive the string value that the caller pass to the event. The bottom one will ignore the value passed by the caller and allows you to specify the string value statically in the inspector.
okay thanks. i didnt write the script btw, but i see the same method is listed twice now, and one of them lets me type the string. thnx ^^
Answer by logicandchaos · Jan 27, 2020 at 05:37 PM
If it uses an InputField then it should be defined somewhere in your scene hierarchy, you should be able to set it there.
Your answer
