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 I_screw_too_much · Jun 18, 2015 at 09:44 PM · c#unity5

How to destroy a specific Game Object clone in scene?

used array of game object like this and selected prefabs in inspector

 public GameObject[] Cards = new GameObject[52];

instantiated game object with this

 Instantiate (Cards [i], new Vector3 (xVal, yVal, zVal), transform.rotation);

 

I tried to destroy game object like this

 Destroy (Cards[handOne]);
 Destroy (Cards[handTwo]);

and got this error

Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate (theObject, true); UnityEngine.Object:Destroy(Object) test:split() (at Assets/test.cs:367) UnityEngine.EventSystems.EventSystem:Update()

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

2 Replies

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

Answer by pako · Jun 19, 2015 at 08:37 AM

Your code, as is, tries to destroy the GameObjects held inside the Cards array at index positions handOne and handTwo, not the clone created by Instantiate () in the scene.

If you want to destroy the clone, you need to hold a reference to it. The Instantiate function returns the cloned object, as an Object, so you also have to cast it to GameObject in this case:

 GameObject cardClone = Instantiate (Cards [i], new Vector3 (xVal, yVal, zVal), transform.rotation) as GameObject;        
 Destroy(cardClone);


I'm not quite sure what exactly you want to do, but I'm guessing you might want to have a second array, say CardClones, in which you'd add the clones. e.g. something like:

 GameObject[] CardClones = new GameObject[52];
 
 //I guess you have an iterator here to increase 'i' as you create the clones
 CardClone[i] = Instantiate (Cards [i], new Vector3 (xVal, yVal, zVal), transform.rotation) as GameObject;


To destroy a CardClone, first hold it in a temp reference, then set it to null in the clones array (if necessary), and then destroy the GameObject that you hold in the temp reference:

 GameObject tempCard = CardClone[handOne];
 CardClone[handOne] = null;
 Destroy(tempCard);
Comment
Add comment · Show 1 · 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 NikoWave · Jan 03, 2019 at 02:24 AM 1
Share

Thank you so much sir! The idea behind the answer works like a charm in my project!

avatar image
3

Answer by zaid87 · Jun 19, 2015 at 01:24 AM

This is probably because you're trying to destroy the original prefab (Cards[]) which is an assets and not the Game Objects. If you want to make Game Objects and destroy them and a later (unknown) time, you need to have a variable that point to that Game Object and not at the prefab. For example, when creating:

 GameObject newCard = Instantiate (Cards [i], new Vector3 (xVal, yVal, zVal), transform.rotation) as GameObject;

and when destroying it:

 Destroy(newCard);

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

HOW i SOLVE ROOT MOTION PROBLEM WITH ANIMATIONS MADE IN UNITY 0 Answers

How can i auto assign Public value VirtualJoystick to Prefabs 0 Answers

UMA Calculate Head Collider at Runtime 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