Scripting: Enable/Disable

Step 1
In this object we are going to learn how to enable and disable objects with scripting!
Step 2
Load/Open your project from the previous lesson. You should have a moving player and a next level door.
Step 3
Create another level Scene, I named mines as level 4. And make sure it has your player, FirstpersonCamera & Canvas.
Temp1545932011 Temp1545932179
Step 4
Delete Main Camera and Directional Light.
Step 5
Let's make it dark by going to Window>Lighting>Settings...
Step 6
Set Intensity Multiplier to 0, and choose a night skybox. I chose SkyMidnight from this package.
Temp1545932340 Temp1545932684
Step 7
Our model looks too lit in the dark, so change your model's shaders to something that matches with the lighting.
Temp1545932738 Temp1545932764
Step 8
I changed mines to Standard with 0 Smoothness and 0 Metallic.
Step 9
Oh no, now our player is completely dark! Luckily Unity has a switch to turn off lighting sources.
Step 10
Add a spotlight to our player object (as a child object)
Step 11
Give that spotlight more range and intensity
  • Range = 15
  • Intensity = 2.5
Step 12
Create a plane with a x/y scale of 3 under the player.
Step 13
Create some 3D objects on the plane.
Check Your Work
Step 14
Press play and check to see if this all works!
Step 15
Setting up is finished, so let's start creating the script to enable/disable your flashlight!

Create a C# Script called "ToggleWithButton" in your project panel

Step 16
Add these lines of code for your ToggleWithButton.cs script.
Step 17
Let's see what each of these code segments do.
Step 18
Go back to Unity, Add this ToggleWithButton.cs script to your player, our script should look like this.
Step 19
For button put any button you want on the keyboard, and for Target; Click and drag your spotlight to this slot.
  • The spotlight should be the one that you already attached to the player.
  • Use any button for the light switch, I used "u"
Check Your Work
Step 20
Press Play and press your selected button. I chose "u", so whenever I press "u", it switches on/off the flashlight!
Step 21
Let's work on another script to switch enable/disable between two objects. Load back to your level1 scene.
Step 22
Create a new script in your project panel. Name it as "SwitchBetween".
Step 23
This is a lot of code, let's copy this into your script and then we'll see how it works.
Step 24
Here's what each piece of code does, You may want to click on this image to make it bigger to read.
Step 25
Okay now let's try this out. Make a 3D cube, place it where your player can touch it, attach the SwitchBetween.cs
Step 26
Create 2 more cubes next to it and then select our main cube. Now click and drag the two cubes into the script.
Check Your Work
Step 27
Two green lines should have formed. Showing us that these two cubes are now connected.
Step 28
Choose one cube and disable it.
Check Your Work
Step 29
Press play and touch the main cube to test if this works!
Step 30
Can you create a switch/maze game to get to your next level door?

Extra Challenge: How would you Enable/Disable more than 1 object using the same switch?

  • Hint: Parenting
Step 31
Next we'll do a key scavenger hunt to open doors and locks! Or activate a next level door! First download a key 3D model
Step 32
Create a new C# Script, name it "EnableNext"
Step 33
Open the script and type these code segments inside the script.
Step 34
Let's see what this code does...
Check Your Work
Step 35
Attach the script to one key, make it linked to another disabled key. Press play to see if this works!
Temp1546042949 Temp1546043076

If it doesn't work, make sure the key has a box collider since we have to touch a collider to activate the function.

Step 36
Make a Scavenger hunt game where you have to collect all keys in order to unlock the next level door!
Step 37
You are finished with this lesson! Click on submit and go to the next lesson on Spawning!
Scripting: Enable/Disable Info


MVCode Clubs

Created By

Nepgear tina (instructor)


Unity Advanced

Access Level


For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach