I’m trying to write a script that will allow a component to be attached to the script from the Unity editor, such as an audio clip or an animation clip.
Any ideas?
I’m trying to write a script that will allow a component to be attached to the script from the Unity editor, such as an audio clip or an animation clip.
Any ideas?
First, animation and audio clips are not components, they are assets.
Components are like the Animation Component that displays and controls an animation clip or the Audio Source component that plays audio clips.
But, if you want to be able to set assets in a script in the Unity editor, this is how you do it.
Public variables are automatically included in the inspector in the Unity Editor.
So, all you have to do to attach assets is declare a public variable in the script and tada.
Eaxample(C#):
class AnimationLoader : MonoBehavior
{
public AnimationClip animationClip;
void Update()
{
//do something
}
}
Well, if the script is attached to a gameObject that has other components (such as an animator or an audioclip) most of the time you can access those components by simply calling them using the appropriate expression (which depends on what you are trying to accomplish). If you want to access a component that is not part of the gameObject just create a variable, something like this: var componentImTryingToAccess : Name of the component; Then on the inspector you'll have to assign the gameObject which holds that component. Is that what you wanted to do?
– AlejandroGorgal