Scripting: Enable/Disable

Planning
Step 1
In this object we are going to learn how to enable and disable objects with scripting!
Capture2
Step 2
Load/Open your project from the previous lesson. You should have a moving player and a next level door.
Temp1545931518
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.
Temp1545932221
Step 5
Let's make it dark by going to Window>Lighting>Settings...
Temp1545932269
Step 6
Set Intensity Multiplier to 0, and choose a night skybox. I chose SkyMidnight from this package.
Temp1545932340 Temp1545932684
Planning
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.
Temp1545933083
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)
Temp1545934712
Step 11
Give that spotlight more range and intensity
Temp1545935349
  • Range = 15
  • Intensity = 2.5
Step 12
Create a plane with a x/y scale of 3 under the player.
Temp1545935224
Step 13
Create some 3D objects on the plane.
Temp1545935389
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!
Temp1545941208

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

Step 16
Add these lines of code for your ToggleWithButton.cs script.
Temp1545943739
Information
Step 17
Let's see what each of these code segments do.
Temp1545944155
Step 18
Go back to Unity, Add this ToggleWithButton.cs script to your player, our script should look like this.
Temp1545944226
Step 19
For button put any button you want on the keyboard, and for Target; Click and drag your spotlight to this slot.
Temp1545948485
  • 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!
Planning
Step 21
Let's work on another script to switch enable/disable between two objects. Load back to your level1 scene.
Temp1545949537
Step 22
Create a new script in your project panel. Name it as "SwitchBetween".
Temp1545960077
Step 23
This is a lot of code, let's copy this into your script and then we'll see how it works.
Temp1545995080
Information
Step 24
Here's what each piece of code does, You may want to click on this image to make it bigger to read.
Temp1546000695
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
Temp1546000867
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.
Temp1546001150
Check Your Work
Step 27
Two green lines should have formed. Showing us that these two cubes are now connected.
Temp1546001187
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!
Challenge
Step 30
Can you create a switch/maze game to get to your next level door?
Temp1546001771

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

  • Hint: Parenting
Planning
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"
Temp1546039456
Step 33
Open the script and type these code segments inside the script.
Temp1546042770
Information
Step 34
Let's see what this code does...
Temp1546042890
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.

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

Account

MVCode Clubs

Created By

Nepgear tina (instructor)

Course:

Unity Advanced

Access Level

premium

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