Question by
alemas · Mar 28, 2016 at 12:33 AM ·
buttonclick objects
Accessing public variable with the On Click() function list in Inspector
I am trying to detect a UIButton click event in Unity 5.3.31f. I attached a C# script to an empty GameOject, and I am trying to access a bool variable in the script using the On Click() function list in the inspector.
using UnityEngine;
using System.Collections;
public class InputState : MonoBehaviour {
public bool actionButton;
}
The function is visible in the pool down menu, but the actionButton variable does not appear in the menu. I am not sure what I am doing wrong.
screen-shot-2016-03-27-at-51911-pm-2.png
(60.9 kB)
Comment
Answer by paulomuggler · Apr 19, 2017 at 09:02 PM
This comes up for me as well once in a while; I'm not sure if class fields can appear at all as targets of event callbacks in the inspector.
What I do is use a public property with only a setter, that refers to the field I declared, like so:
public bool thisIsMyField = true;
public bool myFieldAccessorProperty {
get {
// getter is not required, I add it for consistency's sake when accessing
// the value from other scripts
return thisIsMyField;
}
set {
thisIsMyField = value;
}
}
property setters will appear normally in the OnClick() function list in the inspector.