Lesson 11 – Enabling and Disabling Components

Starting with Lesson 11, a Github page of these lessons projects will be provided for your convenience, if ever needed: https://github.com/thee0ry/UnityScripting_BeginnerGameplayScripting

Undeniably,there will be points in time where you have to enable/disable an object within a video game.

A perfect example would be a light switch. You want to enable a light object if the switch is pressed to On and disable a light object once a switch is pressed to off. This light object is in your Editors hierarchy, thus making it a part of the Scene once the Play button is pressed.

In this Scene, we have created a script and attached it to the light object as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnableAndDisableLight : MonoBehaviour
{
	protected Light _light; // declaring the variable for the game object component

	// Use this for initialization
	void Start ()
	{
		_light = GetComponent<Light> (); // referencing the game object component that the script is attached to with this variable
	}

	// Update is called once per frame
	void Update ()
	{
		if (Input.GetKeyDown("space")) // if the user presses down on the space button, run the code in the brackets
			{
			_light.enabled = !_light.enabled;
			/* _light.enabled equals (!)NOT the current bool value of _light.enabled.
			In other words, it will equal the opposite of the current bool value. */

			print ("Success! This new lamp works, man!"); // print this success message to the Console to verify it's going through
			}
	}
}

This script applies a behavior to our Light GameObject component so that we may Enable/Disable the object when the user clicks on the space button. We also print a little fun message to ourselves in the Log, just to make sure it’s working.

Note: Notice that the game object does not leave the hierarchy in your Editor, or even leave the Scene. When disabled, it stays active in the scene so that you may access it again later. This is a perfect time to visually verify the info I’ve been feeding down your throat for about the third time now! 😀

Lastly, please note that Enabling/Disabling using dot enabled is not limited to just in-game objects. Entire scripts can be Enabled/Disabled as well because they are added to game objects as components.

Conclusion:

I would say that this is a very straightforward lesson, and its concepts should not be foreign to you even if you’re just a player and not a game dev’: not all things need to be enabled all the time. We need scripts to enable objects **only** when and if we need to make them available to the player. Truck on forward!

Study Resource:
https://unity3d.com/learn/tutorials/topics/scripting/enabling-and-disabling-components?playlist=17117

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s