Show/hide object in C#

Hi all, I have recently moved into C# scripting from JavaScript and I would like to know if there is a way of controlling an object in C# so that it will be hidden when the scene starts but will then appear when a collision occurs? This is the script I currently have:

using UnityEngine;
using System.Collections;

public class collideEnemy : MonoBehaviour {
	
void Start () {
	Destroy(GameObject.Find("Cog"));
}

void OnTriggerEnter(Collider other) {
	Destroy(GameObject.Find("Enemy"), 1);
	Instantiate(GameObject.Find("Cog"));
}
}

I was trying to accomplish what I wanted to do with the destroy and instantiate functions, however this gives me a “NullReferenceException” error, I’m guessing because the item I’m trying to Instantiate is already destroyed. Is there another way to do this, or a way around this? I was thinking of calling the function from another script but I’m unsure how to do this in C# or if this would even work. I’d appreciate any help!

It’s unecessary to destroy and instantiate an object whe all you want to do is show/hide. You can either play with the gameObject.active boolean (mind that the OnTriggerXXX functions won’t be raised) or with the renderer.enabled one.

I don’t think calling it from another script would help. Presumably you actually need Cog to be a prefab - then have a variable of type GameObject in your class that you assign to the Cog prefab using the inspector.

    GameObject cogPrefab;
   ...

   Instantiate(cogPrefab, other.transform.position, Quaternion.identity);

To create the prefab just drag the game object from the hierarchy to the project view.