- Home /
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!
Answer by whydoidoit · Nov 10, 2012 at 10:02 PM
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.
Thanks for the help. I made the cog a prefab and named it cogPrefab, and changed my script to this:
using UnityEngine; using System.Collections;
public class collideEnemy : $$anonymous$$onoBehaviour { GameObject cogPrefab;
void Start () { Destroy(GameObject.Find("cogPrefab")); }
void OnTriggerEnter(Collider other) { Destroy(GameObject.Find("Enemy")); Instantiate(cogPrefab, other.transform.position, Quaternion.identity); } }
However I now get an error saying "collideEnemy.cogPrefab is never assigned to, and will always have its default value 'null". When I run the scene, the cog destroys fine but does not instantiate after the collision and gives me a "NullReferenceException" error. Any ideas?
You don't need to delete the cogPrefab! Just remove that from your scene after you created the prefab.
You need to make cogPrefab variable in collideEnemy public and drag your cogPrefab from the project window onto the variable in the inspector.
Thanks, this worked perfectly (once I'd figured out what to do!). If anyone's trying to do the same thing this is what I did.
I put this after the $$anonymous$$onoBehaviour line:
public GameObject cogs;
Then inside my function and inside the if statement I put
Instantiate(cogs);
I realise this is basically the same code $$anonymous$$ike gave me, it just took me a while to understand it!
Answer by FakeBerenger · Nov 10, 2012 at 10:05 PM
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.
Your answer
Follow this Question
Related Questions
Destroying Objects. 1 Answer
Is there anyway to delete clones that are local variables? 1 Answer
References to GameObject become null 1 Answer
[Solved]Instantiated item won't get destroyed until "picked up" again. 1 Answer
[Solved] Unable to instantiate an object from within the OnTriggerEnter callback. 1 Answer