Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MH1 · Jun 28, 2013 at 09:05 PM · c#prefabdestroycreatesend

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Immanuel-Scholz · Jun 28, 2013 at 09:09 PM 1
Share

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)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MH1 · Jul 01, 2013 at 01:28 AM 0
Share

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);
         
             
     }
 }
avatar image iwaldrop · Jul 01, 2013 at 03:29 AM 1
Share

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.

avatar image iwaldrop · Jul 01, 2013 at 05:02 PM 1
Share

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;
 }
avatar image iwaldrop · Jul 01, 2013 at 07:50 PM 1
Share

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.

avatar image iwaldrop · Jul 24, 2013 at 03:25 PM 1
Share

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. :)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges