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
2
Question by ina · Jan 20, 2012 at 05:03 PM · gameobjectdestroyinstance

Destroy a gameobject instance but also save it in a new variable

It seems like when you Destroy(gameObjectInstance), but first save its attributes in a new variable newvar = gameObjectInstance, the Destroy does not work anymore...

How do you destroy a gameobject instance but also save it in a new variable - something for an undo/redo system.

Comment
Add comment · Show 2
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 Jessy · Jan 20, 2012 at 05:21 PM 1
Share

Your variable is not a copy of the Game Object's data; it's a reference to the object. So if you Destroy it, the reference is null. This works for Editor undo: http://unity3d.com/support/documentation/ScriptReference/Undo.RegisterSceneUndo.html

avatar image ina · Jan 20, 2012 at 05:34 PM 0
Share

What if you want to do a runtime undo/redo ? ... Also, even though it's a reference, it seems that when you copy the reference and destroy the original, the gameobject still remains in the scene. That's what I meant when I said the Destroy() doesn't work anymore.

1 Reply

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

Answer by ThePunisher · Jan 20, 2012 at 09:03 PM

Well first of all, I would have thought that keeping a reference to a game object would keep the Destroy() method from being able to destroy an object, but that doesn't seem to be the case. I tried keeping a reference to a GameObject and then destroying with Destroy() and DestroyObject() and in both cases it simply destroyed the GameObject even though I had a reference to it. Try creating an empty object (CTRL+SHIFT+N) and adding the following script to it. Then create a cube and drag it over to the public member of the script within the inspector. Run it. You should see the GameObject be destroyed. This leads me to believe that you're reference to the object may not be valid? Are you sure your reference isn't to a script rather than the GameObject itself?

 using UnityEngine;
 using System.Collections;
 
 public class GuiThing : MonoBehaviour
 {
     public GameObject obj;
     
     private GameObject objReference;
     
     
     // Use this for initialization
     void Start ()
     {
         objReference = obj;
     }
     
     // Update is called once per frame
     void Update ()
     {
     
     }
     
     void OnGUI()
     {
         if(GUI.Button(new Rect(Screen.width/2 - 125, 0, 250, 50), "Destroy object"))
         {
             Destroy(obj);
         }
     }
 }

In any case, perhaps you could create a clone of the original object, destroy the original object, and disable the clone until the undo button is hit (at this point you would enable the object again). Or you could simply disable the original GameObject and enable it once it was needed again. Hope this helps

Comment
Add comment · Show 2 · 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 ina · Jan 21, 2012 at 04:48 PM 0
Share

That's so weird.. I wonder if this is one of those UnityScript / JS only things...

avatar image ThePunisher · Jan 21, 2012 at 05:31 PM 0
Share

So you're using JS? I would think they'd work the same, but that's not always the case. Did you make sure that your reference your keeping is to a GameObject rather than a script?

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

6 People are following this question.

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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

What is wrong with this script? 2 Answers

Instantiated GameObject being deleted when destroying original 1 Answer

Destroying Game Object On MouseButtonDown + Distance Collision 0 Answers

How to Play 3D Sound and Keep Script on Multiple Prefabs after Destroying a gameObject? 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