Storing any component inside a single variable.
Hello!
I was wondering if there was any way to store any kind of component inside a single variable.
I would like to have a script that disables any component inside the OnDisable() method just setting the property .enabled to false.
Is it possible? Thanks!
Answer by Hellium · Feb 09, 2020 at 12:34 AM
Behaviour[] behaviours = GetComponents<Behaviour>();
foreach(var b in behaviours)
b.enabled = false;
However, GetComponents<Behaviour>() won't retrieve components such as colliders (because they are not Behaviours) and you will need to do it separately
Collider[] colliders = GetComponents<Collider>();
foreach(var c in colliders )
c.enabled = false;
Hello Hellium, thanks for the answer.
I have tested it and it works fine but I wanted to avoid exactly what you said, having to retrieve some kind of components separately.
Anyway, its a good a approach. Thank you!
Otherwise, you could call GetComponents<Component>() and use reflection to see if the component have the enabled property, but of you could avoid it, it's preferable.
(Check if the PropertyInfo is not null before calling SetValue)
Your answer
Follow this Question
Related Questions
How to alter Right/Top Value from RectTransform 0 Answers
Gravitation 0 Answers
use a function twice 1 Answer
How to take user input, convert it integer, and print it on screen? 1 Answer