I wanna destroy an object from a script

Hello

I know the destroy command

Destroy(gameObject);

Which delete the object the script is attached to.

But im looking for this function. Object A have script A, in Script A it tells me to destroy object B

Im trying to do that with this code:

Destroy(iconUpgradeCurrent);
Destroy(iconSellCurrent);
Destroy(iconBackCurrent);

but im getting this error

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

How can i disobey the “Destroying the transform component is not allowed.”-rule

say your Destroy(iconBackCurrent) for example.

its possible that you have this iconBackCurrent referenced as Transform type at the top of the script?
for destroy to work, you have to give it the gameobject as opposed to one of the components, on a gameobject.

either declare it as a gameobject type rather than a transform, or try ammending the destroy to the following to see if it works.

Destroy(iconBackCurrent.gameObject);

see how it goes :slight_smile:

NB : but, if its a public reference in a slot in the inspector and you change the referenced type, you will have to drag it back into the inspector as you will get errors otherwise…

What are “iconUpgradeCurrent”, “iconSellCurrent”, and “iconBackCurrent”. According to your error it sounds like they’re not GameObjects…in which case you can’t use Destroy…there is no way to disobey that.