Make Objects Appear or Disappear WITHOUT CODE in Unity
Want to make games? Scared of code? Come on in, we’re glad to have you!
Today we’re going to make an object appear or disappear at the click of a button. You can even make the button itself appear or disappear!
This tutorial will assume you have your canvas and at least one button set up and ready to go. If you don’t know what that means, or how to do it properly, start with this tutorial.
Ready? On with the show.
Make Objects Appear or Disappear
Click on your button, then add a new ‘On Click’ function by clicking the plus sign.
Choose an object that you want to appear or disappear when the button is clicked. I want the button to disappear when you click it, so I’ll put it in the box in the second row, currently labeled ‘None’.
Once you’ve set your object, go to the function menu and choose ‘Game Object’. From that sub-menu choose ‘Set Active (bool)’.
Now a check box appears so you can choose whether you want your object to appear or disappear when the button is clicked. When the box is empty, your object will disappear when the button is clicked. When the box is checked, your object will appear when the button is clicked.
I want my button to disappear, so I’m going to leave the check box empty.
If you want your object to appear, when the button is clicked, then you need to make sure the object is, well, disappeared in the first place. It can’t appear if it’s already there!
Your object still needs to be in the hierarchy for this to work, so Unity gives us a fancy tool to turn objects on and off, so to speak. Just bring up the object in the inspector and click the check box by its name!
If the box is checked, the object appears onscreen when you push play. If the box is empty, the object does not appear onscreen when you push play, and you can use a button to make it appear.
When you’re finished, you can click play at the top of the screen to test your work!
Troubleshooting
If your object doesn’t appear, check two things:
1) Make sure your object is in the hierarchy, not just your project dock.
2) Make sure your object is ‘disappeared’ (set active: false) when you play the project.
If your object doesn’t disappear, check two things:
1) Make sure your object is in the hierarchy and appears onscreen when you play the project.
2) Make sure the check box is empty, not checked.
Taking it Further
Remember that each button can have more than one OnClick function.
You can make two objects appear at once, or make one object disappear while another one appears in the same place.
And guess what: the text on a button is a separate object; you can make it disappear independently from the button it’s on! (Because the text is a child object, it cannot appear unless the button it’s on is also, well, appeared.)
Finally, remember that you can change the button sprite to anything you want! It doesn’t have to be a white rectangle (unless you want it to be).
Conclusion
Now you have the tools to make a game, so go make one! You could make a whack-a-mole game, or an Easter Egg hunt. You could even make a visual novel or quiz game! Use your creativity.
But if you’d like to do even more without code, check out the other entries in this series:
Setting Up Buttons WITHOUT CODE