Skip to content

Instantly share code, notes, and snippets.

@samsheffield
Created October 3, 2021 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsheffield/763bda5893c284d2148fefa04a2f5b3b to your computer and use it in GitHub Desktop.
Save samsheffield/763bda5893c284d2148fefa04a2f5b3b to your computer and use it in GitHub Desktop.
(De)Activating GameObjects
Here is a bonus Unity example for 2D Game Design F21. Let me know what else you need!
======================================================================================
(DE) ACTIVATING GAMEOBJECTS
Activate another GameObject on Start example: ShowOnStart.cs
Deactivate this GameObject on action (press space) example: HideOnAction.cs
Example Unity Project: https://drive.google.com/file/d/1qhtZZOpn83fH7DKXjtUJ8WmbGrK62By2/view?usp=sharing
Important:
1. ShowOnStart requires the GameObject you want to activate deactivated in your scene (uncheck the top-left box in its Inspector)
2. ShowOnStart requires Another GameObject to activate it because scripts/components are also deactived when their GameObject is inactive
3. HideOnAction demonstrates by using a key press, but you can create a bool variable and then check to see if it is true to signal the action as well
// This sets this gameobject active
gameObject.SetActive(true);
// This sets another gameobject (linked with a variable) incative
variableName.SetActive(false);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HideOnAction : MonoBehaviour
{
// You can set this to another key in the Inspector
public KeyCode hideKey = KeyCode.Space;
// Update is called once per frame
// Add any code here that needs to loop Constantly
void Update()
{
// Check to see if some condition is true...
if(Input.GetKeyUp(hideKey) == true)
{
// ... then hide this gameObject by deactivating it
gameObject.SetActive(false);
/* Why isn't my code running once I deactivate this gameobject?
* Scripts and other components will also be deactivated
*/
/* Why gameObject and not GameObject?
* gameObject refers to This One while GameObject means the entire class of gameobjects
*/
/* What if I want something else to trigger this to deactivate?
* Use a bool variable and do something to eventually set that true
* Replace the input condition in if() to check your variable like if(gameOver == true)
* */
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShowOnStart : MonoBehaviour
{
// Link to the gameobject in your scene
public GameObject activateThisOne;
// Start is called before the first frame update
// Put any code in here that you want to run Just Once when your scene starts
void Start()
{
// To set this gameobject active...
activateThisOne.SetActive(true);
/* Why can't you just put this directly on the gameobject itself?
* When a gameobject is inactive, it's scripts are also inactive
*/
/* Why might you do this instead of just keeping the active box checked in the Inspector?
* Sometimes having something always active in your Scene View can make things cluttered
* Sometimes we want something to happen when we spawn (instantiate) clones of a prefab
*/
/* Why am I getting a NullReferenceError?
* You forgot to set the public GameObject variable in the Inspector
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment