- Home /
Create destroyed prefab
I have a prefab, which is destroyed in a C# script.
I want to send this object back onto the screen after the player presses a key.
Any suggestions?
Can you describe more what you trying to do? An destroyed object is .. well.. destroyed. It can not be displayed, since it doesn't exist anymore.
Or are you speaking of the Undo-system in the editor and you want to undo some actions done in C# scripts? That's only possible if the C# script that destroys your object follows the Undo/Redo framework rules (e.g. by calling Undo.RegisterSceneUndo()
first)
Answer by iwaldrop · Jun 29, 2013 at 06:42 AM
Don't destroy it, just disable it. When you want it back again, enable it. Destroying/Instantiating are two of the single most expensive operations that you can perform, so try to avoid them when you can. Check it:
gameObject.SetActive(true-or-false);
I tried that, but I am getting an error:
UnityEngine.GameObject' does not contain a definition for SetActive' and no extension method
SetActive' of type `UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
Here is my script:
using UnityEngine;
using System.Collections;
public class aim : $$anonymous$$onoBehaviour {
public static int cheat = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (cheat==1)
//Destroy(this.gameObject);
gameObject.SetActive(false);
float h = Input.GetAxis ("Horizontal") * Time.deltaTime * moveSpeed;
float v = Input.GetAxis ("Vertical") * Time.deltaTime * moveSpeed;
transform.Translate (h,v,0);
}
}
Are you using Unity version 3.x or 4.x? SetActive was introduced in 4.0. If you're using a version previous to 40, do the following:
gameObject.active = false;
If you're using Unity 4 and you're getting this error then write back, because Game Objects most certainly do have this method: see here.
You're very likely calling this from the Update loop and shouldn't. Update isn't running on disabled objects. $$anonymous$$ove it to a method and call it another way.
GameObject objectToSetInactive;
void Cheat()
{
objectToSetInactive.active = !objectToSetInactive.active;
}
Well, part of why I didn't is because your code doesn't show how you set cheat to 1. If it's from an external script, then just make Cheat() public and call it ins$$anonymous$$d of setting an int to 1. That's the way you should have done it from the beginning, because checking a flag every frame is pointless when you can just call a method.
Of course, SetActive is just a regular old public method. In order to call it you just have to get a reference to the game object. The question of how to do it is, for some reason, asked almost everyday on unity answers, so a quick search should give you loads of examples.
Edit: I forgot you're using 3.x. In your case, the active variable is also public, so just replace any SetActive examples with that. :)
Your answer
Follow this Question
Related Questions
Destroy Prefab with UI Slider 1 Answer
How to store a prefab in a variable 1 Answer
Trouble with destroying an instantiated prefab 2 Answers
Destroying Objects. 1 Answer
How to destroy Instantiated objects (C#)(not last instantiated one) 0 Answers