Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 AnotherGuy_Evan · Oct 12, 2021 at 08:56 PM · clonedestroy objectinstantiate-game-object

Looking to destroy clone of gameobject without destroying original

Basic idea: Crate prefab spawns.

If you double click on the crate, it instantiates a larger version of that, a sort of 'zoom in' on it, to show details.

If you then click outside of the panel, it destroys the instantiated clone, as well as hiding the UI panel that has the other details on it.

Currently this destroys both the clone and the original, which isn't what I want, I just want to destroy that clone.

   if (Input.GetMouseButtonDown(0))
             {
                 float timeSinceLastClick = Time.time - lastClickTime;
     
                 Ray mouseRay = GenerateMouseRay();
                 RaycastHit hit;
     
                 if (Physics.Raycast(mouseRay.origin, mouseRay.direction, out hit))
                 {
                     if (hit.transform.tag == "crate")
                     {
                         if (timeSinceLastClick < clickThreshold)
                         {
                             doubleClick = true;
     
                             Debug.Log("Double click");
                             Debug.Log(timeSinceLastClick);
     
                             crateHighlighted = true;
                             currentCrate = hit.transform.gameObject;
                             ToggleHighlight(currentCrate);
     
                             Time.timeScale = 0;
                         }
     
                         lastClickTime = Time.time;
     
                         //currentCrate set to gameObject that was hit
                         currentCrate = hit.transform.gameObject;
                     }
                     else
                     {
                         if (crateHighlighted)
                         {
                             ResumeTime();
                         }
     
                     }
                 }
             }
     
        void ToggleHighlight(GameObject selectedPackage)
         {
             highlightedCrateTemp = selectedPackage;
     
             Debug.Log(highlightedCrateTemp.GetComponent<Crate>().itemName);
     
             GameObject highlightedPackage = Instantiate(highlightedCrateTemp, highlightedCratePos, highlightedCrateTemp.transform.rotation);
             Destroy(highlightedPackage.GetComponent<Rigidbody>());
             uiManager.DisplayHighlightedDetails(highlightedCrateTemp);
         }
     
         void ResumeTime()
         {
             Time.timeScale = 1;
             uiManager.highlightedObjectDetails.SetActive(false);
             highlightedCrateTemp.SetActive(false);
             Destroy(highlightedCrateTemp);
         }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aaron4202 · Oct 12, 2021 at 11:55 PM

It looks like you are setting your crate to be temp and you are instantiating it under a local variable and then destroying your temp variable. You will use your function variable to define what will be instantiated not your temp variable and we will make your temp variable your clone So....


Remove your line

 GameObject highlightedCrateTemp = selectedPackage; 

under your ToggleHighlight function.


And change


GameObject highlightedPackage = Instantiate(highlightedCrateTemp, highlightedCratePos, highlightedCrateTemp.transform.rotation);


to

 GameObject highlightedCrateTemp = Instantiate(selectedPackage, highlightedCratePos, highlightedCrateTemp.transform.rotation);



And it should do the trick.

Comment
Add comment · 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

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

126 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 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 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 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 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 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 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 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

Error CS0029 - Trying to destroy an object and then instantiate it again? 1 Answer

Following Object can't find newly Instantiated Object (Solved) 1 Answer

Instatiated objects should have all the same name. 2 Answers

Destroy all clones 2 Answers

How to destroy a clone (instantiate in scene) in the their own script 1 Answer


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