how do you reference a button as a variable? Code (CSharp): public UnityEngine.UI.Button leftbutton; doesn't appear to show up in the inspector.
Tried it and the above works for me. However (personally) I would just use a standard GO and then manage a reference internally to the Button Component. Allows you to verify it is a button and also access anything else on the GO if need be.
What? How does it allow you to verify it's a button? If a Button component is dragged in the inspector how is that not verified? How can one not access anything on the gameobject with Button? Code (csharp): Button.GetComponent<Rigidbody2D>().transform.position = Vector3.up * 0xABCDEF;
It's just semantics @_Daniel_ , as they all derive from MonoBehaviour, the behaviour to put above would work regardless. It all depends on your use case, if it HAS to be a button, then having a property of Button is fine. If you intend to use it for other things (not just a button) then make it a generic GO. (it's stored internally as a GO anyway) In either case, if you want to use the Button component in code, you will still have to do Button.GetComponent<Button>. Having it as a property of button just helps the editor validate what you attach to it. Point of debate really, it is just my preference.
Point is, you don't have to do button.GetComponent<Button>(), if you have a serialized Button field in the inspector.
solved: I just needed stuff in the void Start() block. Apparently those public variables don't show up without stuff in there or something. I don't know, so correct me if I'm wrong.
Last time I tried to drag a button over a public variable in the inspector it didn't work. Both when I tried using the "GameObject" type and the "Button" type itself. Does it work now somehow?
It should work. Make sure it's a public class member, and also make sure you don't make it a property by adding { get; set; } after it. For some reason, unity disregards these. an example would be: Code (CSharp): using UnityEngine; using UnityEngine.UI; public class MyGameObject : MonoBehavior { public Button Mybutton = null; } You should now be able to drag over any GameObject that contains a Button component onto it. Do note it may take Unity's IDE a moment to add the property. If you look in the bottom right of the screen you'll see the little "spinner" while it's compiling changes. Sometimes on larger projects it could take 15 seconds or more to finish. I wouldn't recommend just generically using the GameObject type if you explicitly require a Button component. This will make your script inspector properties a ton harder to maintain and understand.