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
1
Question by theqtuber · Jul 13, 2012 at 09:59 PM · errorgameobjectobjectdestroy

Why Can't I Destroy This Object?

Hi. I have a problem with the Destroy() function in my program. I know it takes a gameobject, but I still get the error below:

Can't destroy Transform component. If you want to destroy the game object please call 'Destroy' on the game object instead. Destroying the transform component is not allowed.

I don't know what it means. The only point where I have the Destroy() function is when I wrote,

Destroy(hairset);

Earlier I define hairset:

static var hairset : GameObject;

The only reason I could think of for this not working is the fact that it might not work except with the gameobject that the script is attached to, but I also don't see the logic in that. If this is true, then how can I destroy my variable? I just don't understand what it's talking about with the transform component.

Thank you for any help you can give.

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 crogers · Jul 13, 2012 at 10:07 PM 0
Share

you have only mentioned defining hairset. is it a null reference? also, do you really want an object that can change declared as "static" ?

avatar image Bunny83 · Jul 13, 2012 at 10:31 PM 0
Share

Well, the given information tells use that you try to destroy a Transform component because you pass a reference to a Transform to the Destroy function. The error can't have any other reason.

So there are several possibilities:

  • You have another variable, maybe in another scope, called "hairset" and this is of type Transform.

  • It's not the given line that produces the error.

Some more context would help i guess. Also like crogers said, why is it static and where do you set it? It would also be useful to know where and when you call Destroy

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Zantom07 · Jul 13, 2012 at 10:28 PM

Are you using the inspector to define what hairset actually is? If so, you can't attach gameobjects within the inspector, only transforms, so maybe you need to change the definition to this:

 static var hairset : Transform;

And then the destruction to this:

 Destroy(hairset.gameObject);
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
avatar image
0

Answer by Piflik · Jul 13, 2012 at 10:07 PM

It should work if you type Destroy(hairset.gameObject). Although it might be possible that the static property of your variable interferes with this...

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
avatar image
0

Answer by TheDarkVoid · Jul 13, 2012 at 10:29 PM

it would be best to call the Destroy() function on the object you want to destroy.

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
avatar image
0

Answer by untityuserpoint0 · Jul 13, 2012 at 11:55 PM

I am the same user who asked the question. Just a different username. I tried using Zantom's advice (and Croger's advice. It is no longer static) and I'm not getting the same error instead it says, "MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object." Truth is... I never access it again. The route I might take is to just find another way to the solution instead of using Destroy. Is there any other way to get rid of an object without affecting the variable that it is attached to? What I should have explained was that I have two objects (hairset and hair1). Hairset is in the scene and Hair1 is a prefab that is not in the scene. I need to switch them out, but I need to do it so that the prefab, Hair1, is set as Hairset after the original Hairset object is gone. Is that possible?

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 Zantom07 · Jul 14, 2012 at 07:21 AM 0
Share

Can you tell us what the numbers are in the brackets, something like:

$$anonymous$$issingReferenceException: blablahblah... (22,23)

That shows you the error is on line 22, column 23. This should help you pinpoint where your problem lies.

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

10 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

Related Questions

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

Destroy object when transform.position.x > 10. 1 Answer

Having problems destroying an Object 2 Answers

Cannot destroy child object in prefab- Error 1 Answer

Why won't my script load? 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