this may be another beginner question. I have a simple data holding script and want to pass one variable from it as parameter to a function. But in the editor it only shows me the script name, not the option of any variable. What attribute or change do I need to make to make the variable visible for selection? (Passing the whole object works, but I don't like the parsing of the object inside the called method and would just to pass the parameter directly) Picture for easier understanding.

enter image description here


A UnityEvent, like the Button's OnClick event acts on methods, not on variables.

The reason it looks like it's setting variables in the cases you showed is that it's acting on something in C# called a property. That's where you define something like this:

int _internalValue;
public int exposedValue {
    get {
        return _hiddenValue;
    set {
        _hiddenValue = value;

This creates something that looks to your code like it's a variable: I can say exposedValue = 5 or if(exposedValue > 0) for example. But it "de-sugars" to methods: SetExposedValue(5) or if(GetExposedValue() == 5.

This is syntactic sugar to make it easier to write and work with simple getter and setter methods common in certain programming styles/patterns. In practice we'll often put validation/update/copying logic inside these methods, or make the getter or the setter private, to ensure outside interference can't put our internal hidden variables into an invalid state.

C# will let you sweeten this even more for simple cases that don't need extra logic, with an auto-implemented property:

public int exposedValue { get; set; }

Here we don't even have to explicitly declare and manipulate our "backing store" variable _hiddenValue - the compiler generates code equivalent to the example above.

Since the set part of this property is a method that takes one argument, our UnityEvent can call it - as long as the argument (the type of our property) is a primitive type (eg. int, float, string, bool...) or something descending from UnityEngine.Object (eg. GameObject, ScriptableObject, Component/MonoBehaviour/descendents, Material, Texture...)

So your Tile.tag & Tile.name examples are referencing the setter methods of the properties called tag and name, not variables.

Since your TileHelper isn't a primitive type or descended from UnityEngine.Object, we won't be able to assign the whole object with a button click event this way. But we could add some properties to set parts of it at a time:

public TileHelper tileHelper;

public string tileHelper_CorrespondingLevel { 
    set { tileHelper.correspondingLevel = value; } 

This will expose the tileHelper's correspondingLevel field to be set by a UnityEvent.

Or, you could make your helper class derive from ScriptableObject to assign it all at once by reference to an asset in your assets folder.

  • \$\begingroup\$ Thank you for your time. This gave me some insight in how to handle it, although the root problem seems to be something else. The On Click is custom parsing from NGUI and can't handle it if the variable is not a basic one and on the same script. (And no getter needed) But the links gave me an idea in how to extend the code to implement to make it work with custom classes/ struct to get the same result. \$\endgroup\$
    – Zibelas
    May 23 '19 at 7:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .