- Home /
How to handle coupled components in Unity
The idea on ECS architecture is that you have components that are pure data, that are being added to the Entities that are pure collection of Components and then you have systems that depending on the containing component types of each entity either process the entity or not eventually completing some functionality. The benefit is that when you add or remove a component (which is just data), you change the behavior of the Entity.
In Unity things are in different way. You have predefined components for which some functionality is written in the engine. You can also define Scripts which contain data AND behavior. What if that behavior requires data farm multiple Components/Scripts? [In pure ECS it is always like that - several components constitute a single functionality. (e.g. PositionComponent and MoveComponent together result the Entity to move and if one of them is absent, Entity will not move).] I mean I can randomly add and remove components and Scripts, and still each Script in my Game Object should know if it has all the components required to accomplish the functionality. How should I achieve this in Unity?
P.S. I need this so that if I would remove a component/script some functionality will be disable util I add it back.
Answer by meat5000 · May 17, 2016 at 01:53 PM
I found your question a little unclear but I think you are looking for this
http://docs.unity3d.com/ScriptReference/RequireComponent.html
Which will automatically add a required component if flagged in a script.
I dont believe there is one for removing. Its easy enough to Destroy(Component) in your script though.
You probably know that scripts are often linked with GetComponent.
Unity doesnt identify with one Pattern, although it is Object and Component Based. You are pretty much free to mix your patterns as you require them. I'd say Unity is a little different from the Computer Science "Stick to the book" way of doing things.
Answer by YoungDeveloper · May 17, 2016 at 01:55 PM
Why not just execute code on awake and ondestroy?
Your answer
![](https://koobas.hobune.stream/wayback/20220612080958im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
"There is no 'GameObject' attached to the "x" game object' 2 Answers
Is there a way to dynamically attach a script to a GameObject during runtime? 1 Answer
How to add components to other GameObjects through a C# script attached to one? 2 Answers
Can't disable component, little checkbox is missing from inspector 1 Answer
Access water4 scripts via my script? 1 Answer