Enable or disable a property on a GameObject
Learn how to use a Logic script to enable or disable properties on a GameObject.
Read time 1 minuteLast updated 4 hours ago
This example shows you how to set up a script that enables or disables the gravity of a GameObject. You can also use these steps to alter different properties.
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 Cube in your scene
- Right-click in the Hierarchy panel and select Primitives > Cube to create a new Cube GameObject.
- Select the cube. The Inspector window for that cube opens.
- Within the Physics component, disable Use Gravity.
Create a new Logic script
- In the Project panel, select the My Logic folder.
- Select Add (+).
- Select New Script. Unity Studio creates a new Logic script in the folder.
- To rename your script, right click the script and select Rename.
Add the script to the Cube
- Select the Cube from the Hierarchy panel.
- From the Inspector window, 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.
- Create the following variables:
- Unique name : , Type :
Cube_RigidbodyRigidbody - Unique name : , Type :
ButtonObject
- Set up the following logic to enable the gravity when the user clicks the button:
- Drag a When event listener into the Logic Editor.
- Place a Clicked on node in the When node's Event socket.
- Drag the variable in the Clicked on node.
Button - Drag a Set variable value action into the When node.
- In the variables, expand the properties. Drag the useGravity property to the first socket of the Set variable value node. Set its value to
Cube_Rigidbody.True

Example Logic script to enable or disable a property of a GameObject.
Connect your script references
- In the Hierarchy panel, select the GameObject that has your Logic script attached.
- Drag the cube from the Hierarchy panel into the variable references in your script component.
Cube_Rigidbody - Drag the button from the Hierarchy panel into the variable reference in your script component.
Button - Press Play to test your scene. When you click on the button, it activates gravity on the cube.