Is there any way to add extra fields to the built-in components?
For example, if I have an AudioSource, and I want to add another field to specify, say, whether its volume should be randomly adjusted by some amount (a float would suffice to store the value, and I will have another script that will read the value back). Can I add the data somewhere to the actual component? How can I store extra data about components in the scene if they’re not custom components (i.e. I can’t just find the source code and add a new field)?
Components are build to achieve a very specialized task. An AudioSource can playback an AudioClip at the objects position and have some parameters it need to complete it’s task (like volume, pitch, dopplerLevel). Components (especially built-in) can’t and shouldn’t be changed. It’s a modular system, each component / behaviour do what it has been created for.
If you want to add functionality to a GameObject you should write a behaviour script and attach it to the object. If you write a script that changes the volume randomly and it needs to store extra information this behaviour should hold this data, because that’s the place where it belongs to.
That’s actually the whole point of OOP. A class is a collection of data and functions that is created for a specific task. You don’t have to worry about how it performs the task internally all you have to know is how you can use it. That’s why we have the accessibility-modifier so stuff that belongs to the class that is needed internally but shouldn’t be changed be the user is marked as private or protected. Only public fields / methods can and should be used by the user.
Usually to extend a class you can derive your class from another to inherit all of it’s data and behaviour. However all Unity classes are marked as “sealed” because they don’t want you to derive from their classes. Only the MonoBehaviour class can and should be used as “interface” between Unity and your stuff.
@Bunny83
And yet. Often you want to give all components a field for their needs, or at least automate the addition of your script to all objects that have a component. I, at the moment, faced the need to add a field to the PhysicalMaterial, the coefficient of rolling force. And how can I do it? Hanging a script on each object that has a PhysicalMaterial is just a useless task. It is important to add your own field to a component that does not affect itself, but is necessary for my calculations. There was a similar problem about the SoundSource, when a bool field was needed, which would be responsible for whether the NPC hears the sound or not,@Bunny83
And yet. Often you want to give all components a field for their needs, or at least automate the addition of your script to all objects that have a component. I, at the moment, faced the need to add a field to the Physical material, the coefficient of rolling force. And how can I do it? Hanging a script on each object that has a physical material is just a useless task. It is important to add your own field to a component that does not affect itself, but is necessary for my calculations. There was a similar problem about the sound source, when a bool field was needed, which would be responsible for whether the NPC hears the sound or not