Use a button to transform a GameObject
Understand how you can transform a GameObject when you click on a button.
Read time 2 minutesLast updated 2 days ago
Use this example to understand how to scale, rotate, or move a GameObject when the user clicks on a button.
Create a button in your UI Canvas.
- Select UI Canvas in the Hierarchy panel.
- Right-click and select UI > Button to create a new button.
Create a New Logic script.
- In the Project panel, select the My Logic folder.
- Select Add (+).
- Select New Script. Unity Lite creates a new Logic script in the folder.
- Enter a name for your script and press Enter.
Add the script to the GameObject you want to transform.
- Select or create a GameObject from the Hierarchy panel.
- From the Inspector panel, select Add Component.
- Choose your Logic script from the list.
Set up the logic in the Logic Editor.
- In the Project panel, double-click the Logic script to open the Logic Editor.
- Drag a When event listener into the Logic Editor.
- Add a Clicked On event inside the When node. This triggers the logic when the user clicks the button.
- Select Add (+) next to Variables to create a new variable for the button and set its type to Object.
- Drag the variable into the Clicked On socket.
button
- Add an action node to define how the GameObject should change when the user clicks the button. You can choose from:
- ScaleByValue or ScaleToValue to change the size of the GameObject.
- RotateByValue or RotateToValue to rotate the GameObject.
- MoveByValue or MoveToValue to change the position of the GameObject.
- Drag the Me variable, which refers to the GameObject the script belongs to, into the Object socket.
- Enter the value you want to transform:
- For scaling, enter the scale value. For example, if using a ScaleByValue node, a value of doubles the object's size.
1
- For rotation, enter the rotation angle.
- For movement, enter the target position.
- For scaling, enter the scale value. For example, if using a ScaleByValue node, a value of
- Set the transformation time. Set it to if you want the change to happen instantly.
0

Example Logic script to scale a GameObject when a button is clicked.
- Close the Logic Editor.
Link the Button to your script.
- In the Hierarchy panel, select the GameObject that has your Logic script attached.
- Drag the UI Button you created earlier from the Hierarchy panel into the button variable reference in your script component.