- Home /
Add components to a list in inspector and then activate them at runtime?
So I am creating a game which has a build mode where you can place various objects. Some of the prefabs I am instantiating from have scripts that I do not wish to be active during placement, only after placement has been completed. I thought a good solution was to disable these script components by default, and then call a method which activates them upon successful placement.
Perhaps this is not the best approach, and I'm open to alternative suggestions. All input is greatly appreciated.
Here's my inspector. As you can see I have two scripts which I would like to activate later, both of which have been placed in a list already.
[SerializeField]
List<Component> activateOnBuild = new List<Component>();
private void ActivateComponents()
{
foreach (Component comp in activateOnBuild)
{
comp.enabled = true;
}
}
And here's my current code. Obviously "comp.enabled = true" doesn't work but it displays what I'm trying to achieve.
Answer by Hellium · Jan 16, 2020 at 03:40 PM
Replace Component
by Behaviour
. The enabled
property is defined here (and in some other components such as Collider
if I remember correctly, but they don't inherit from Behaviour
)
Your answer
Follow this Question
Related Questions
Disable Weapons Until Bought FPS Creator Kit 0 Answers
I cannot get script component. 3 Answers
How to add a component to all selected objects? 1 Answer
Import assets, linking their components automaticly. 0 Answers
Destroying Components from Editor Script Throwing MissingReferenceException 1 Answer