'How to disable Prefabs in Unity 2D?

Im working on an 2d local multiplayer platformer Game. In the game are obstacles (Spikes), when the player collides with them the player will die. I want the Players of the game to decide if they would like to enable or disable the Spikes (the link leads to an image that will help understanding my problem)by pressing a Key. I already wrote a script for that and added it to my dontdestroyOnLoad GameManager. So all my spikes I built are the same Prefabs. My idea was to disable the main Prefab from the Project folder to disable all the spikes in every scene, until you press a Key to reactivate them again. The Problem is, that only the texture itself in the Project Panel disables and not the Spikes Prefabs in the Hierarchy, because the prefabs turn into regular gameObjects. How can I fix this?

My Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using System.Threading;

public class DisableSpikes : MonoBehaviour
{
[Header("Spikes")]
public KeyCode disableSpikes;
public float time;
public GameObject prefabSpikes;

public bool toggleSpikes = true;

[Header("Player Green")]

public KeyCode disableGreen;
public GameObject prefabGreen;

public bool toggleGreen = true;


[Header("Reset Score")]
public KeyCode resetScore;




    // Start is called before the first frame update
    

    // Update is called once per frame
   


   void Update()
    {
    
     if (Input.GetKeyDown(disableSpikes) && toggleSpikes == true)
        {
            prefabSpikes.SetActive(false);
            Debug.Log("Disable");
            //Thread.Sleep(1000);
            Invoke("SetFalse", time); 
        }
    
    if (Input.GetKeyDown(disableSpikes) && toggleSpikes == false)
        {
            prefabSpikes.SetActive(true);
            Debug.Log("Anable");
            //Thread.Sleep(1000);
            Invoke("SetTrue", time);
        }
        
     if (Input.GetKeyDown(disableGreen) && toggleGreen == true)
        {
            prefabGreen.SetActive(false);
            Debug.Log("Disable");
            //Thread.Sleep(1000);
            Invoke("SetFalse", time); 
        }
    
    if (Input.GetKeyDown(disableGreen) && toggleGreen == false)
        {
            prefabGreen.SetActive(true);
            Debug.Log("Anable");
            //Thread.Sleep(1000);
            Invoke("SetTrue", time);
        }
        
    if (Input.GetKeyDown(resetScore))
        {
            
            ScoreScriptBlue.scoreValueBlue = 0;
            ScoreScriptRed.scoreValueRed = 0;
            ScoreScriptGreen.scoreValueGreen = 0;
            RoundScript.scoreValueRound = 0;
            TimeScript.scoreValueTime = 0;
        }
        
    
    }
    
    public void SetFalse()
    {
        toggleGreen = false;
        toggleSpikes = false;
    }
    
    public void SetTrue()
    {
        toggleGreen = true;
        toggleSpikes = true;
    }
}


Solution 1:[1]

void Update() {

if (Input.GetKeyDown(disableSpikes) && toggleSpikes == true){
  // show
  // renderer.enabled = true;
  gameObject.GetComponent<Renderer>().enabled = true;

}

 if (Input.GetKeyDown(disableSpikes) && toggleSpikes == false) {

  // hide
  // renderer.enabled = false;
  gameObject.GetComponent<Renderer>().enabled = false;

} }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Safrosoft