I want to destroy any graphic component on a gameobject , example is image,raw image, sprite , ect .But i don't have the list of all graphic component so i don't know how to loop through it . The reason is because i want to replace the component with the same type , for example i want to add Text component , but i can't add it if there is already graphic component attached in the gameobject because there can only be one graphic component in a gameobject , so i need to destroy the graphic component first , how do i do it ?

I personally prefer the answer on c# but any programming language is accepted !

  • 1
    \$\begingroup\$ Maybe adding what you're trying to achieve, i.e. the reason why you need this would help you get better answers. \$\endgroup\$
    – Vaillancourt
    Sep 19 '20 at 11:38
  • \$\begingroup\$ Why destroy and add new when you could just modify the existing component in place? Or add a child object with the component you want. \$\endgroup\$
    – DMGregory
    Sep 19 '20 at 13:37
  • \$\begingroup\$ @Vaillancourt i want to add Text component but i can't add it if there's already graphic component such as Image , Sprite , Raw Image , ect . i already clearly said it in the question. DMGregory because the component doesn't exist so i need to add new one but i cant if there is already same type of component in the gameobject , Text is a graphic component Image is a graphic component ,there can only be one graphic component BUT i don't have the full list of graphic component (as far as i know) so i can't delete the existing graphic component if i don't know what it is. \$\endgroup\$ Sep 22 '20 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.