Unity UI Button click

I’m trying to get Button working and have several questions.

  1. How can I achieve Unity UI button’s click get detected from ts side?
  2. How to enable/disable gameobject on button click?
  3. How to open url if button clicked?

I can get click on 3D object, but no luck with UI Button. There are images of code and editor


image.png

Original Post on Discord

by user 267660755443122181

  1. You just use the Unity event, make sure your method name is starting with a lowercase letter → rename OnClick to onClick
  2. You can just use the onClick event as well (just assign the gameObject and set active to false in the UnityEvent
  3. You can use the OpenURL component for example, it’s builtin

Achieved 2) with this code. But I couldn’t understand your way of setting active property
image.png

by user 267660755443122181

For example like this if you want to do it without code
image.png

This would work if you write it like so:

onClick() { 
  if(this.myObj) this.myObj.visible = false;
}

That’s because your field is potentially undefined (that’s the ? in myObj? which is shorthand for myObj : Object3D | undefined;

oooh, I thougth it was this.myObj.active = false

by user 267660755443122181

Where I can read about built in OpenURL? Searched in docs, but no results

by user 267660755443122181

It might not be documented there right now. But you can just add it to an object and enable “Clickable” to open the url on click on the object
image.png