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 QuestionBro · Apr 15, 2011 at 06:47 AM · transformdestroycs1502cs1503

cannot destroy transform

public Object firstPiece;

firstPiece = Instantiate(levelPieces[startinglevelPiece],levelPieceSpawnPointL.transform.position, levelPieceSpawnPointL.transform.rotation);

Destroy(firstPiece);

These are the three line where I use the firstpiece variable directly and yet every time I go to destroy it I get this error "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 have tried casting it as a game object and I have tried everything besides directly tagging the object and destroying the objects with that tag, however I KNOW this should be able to work simply like this, what the hell am I doing wrong, am I high or missing some glaring problem (and I have tried changing the "public Object firstPiece;" line to GameObject that gives me lots more errors and problems than it fixes

Comment
Add comment · Show 4
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 · Apr 15, 2011 at 06:54 AM 0
Share

Have you tried: Destroy(firstPiece.gameObject); ?

avatar image QuestionBro · Apr 15, 2011 at 07:01 AM 0
Share

Yes gives me "error CS1503: Argument #1' cannot convert object' expression to type UnityEngine.Object'" " error CS1502: The best overloaded method match for UnityEngine.Object.Destroy(UnityEngine.Object)' has some invalid arguments " and "error CS1061: Type UnityEngine.Object' does not contain a definition for gameObject' and no extension method gameObject' of type UnityEngine.Object' could be found (are you missing a using directive or an assembly reference?) "

avatar image QuestionBro · Apr 15, 2011 at 07:28 AM 0
Share

To explain a little more firstpeice is a prefab that I instantiate when the player gets to a certain position and destroy when he is so far away if that helps, I am still trying with no results

avatar image ProbePLayer · Jul 08, 2013 at 03:08 PM 0
Share

I used $$anonymous$$arowi's advice and it worked. But hey, it IS confusing, cause in the script reference it doesn't explicitly says that you can access the gameObject through the transform. Anyway, thanks $$anonymous$$arowi.

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by loopyllama · Apr 15, 2011 at 08:03 AM

I bet firstPiece is a Transform. Your code "public Object firstPiece;" then casts it to an Object which indeed does not have a ".gameObject" property. This also explains why your GameObject cast did not work...

Destroy can destroy components or entire game objects. However, you cannot remove the transform component of a game object which is why you got your initial error.

instead of:

Destroy(firstPiece);

try:

public Object firstPiece;
firstPiece = Instantiate(levelPieces[startinglevelPiece],levelPieceSpawnPointL.transform.position, levelPieceSpawnPointL.transform.rotation);
Destroy( (firstPiece as Transform).gameObject );

but that code is not pretty. You might want to fix your "public Object firstPiece;" line and post a question about those errors...you probably don't need to cast your Transform to an Object, which forces you to cast it back to a Transform and access the gameObject to destroy it.

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 QuestionBro · Apr 15, 2011 at 08:29 AM 0
Share

That line does work better, however I go to reuse it soon after to destroy a new Object put into the firstPieceVariable and I get

"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."

and changing the "public Object firstPiece;" line to GameObject gives me errors about missing castes or explicit conversions with my instantation line, is there a quick way to get rid of those? perhaps I am not doing the castes right

avatar image loopyllama · Apr 15, 2011 at 08:46 AM 0
Share

if you need firstPiece to not be destroyed, do not destroy it. Just remove the entire destroy line. You need to post the public object errors in a new question. The forums are the place for an elaborate detailed discussion.

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

1 Person is following this question.

avatar image

Related Questions

Script compile errors with following hack & slash tutorial 2 Answers

Confused about GameObject and Transform 1 Answer

Trying to destroy all children, but they don't disappear (no errors) 1 Answer

Targetting script errors 1 Answer

Transform child object over time and then destroy it. 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