How do I assign an audio clip within the script

I have a variable

var doorOpenSound: AudioClip;

that I would like to initialize within the script. I thought it would be simply

var doorOpenSound: AudioClip = doorSlide; //this variable is in the project view but not in the hierarchy

Can somebody please tell me how to initialize this within the script instead of in the inspector?

Thanks!

Have you tried :idea:

 if(doorSlide){//just a way to say =true
audio.PlayOneShot(doorOpenSound);
}

You still have to assign your clip in the inspector.
What do you mean by seeing the var in the hierarchy?
:smile:

What I mean is that I can see that the resource is in the project, but it is not yet attached to a game object in the hierarchy.

I am trying to do all of this in code rather than havingt to assign the audio clip in the Inspector. Is there some way to do that?

thx

If you want to do it with straight code, I think Resources.Load will do what you need:

Note that your sounds would have to be stored in the Resources folder.

But if you don’t mine assigning it once, you could create an empty GameObject, name it DoorSlide, attach an audio source and assign your doorSlide sound to it.

Then in your other scripts, access it this way:

var doorOpenSound : AudioSource;
doorOpenSound = GameObject.Find("DoorSlide").GetComponent(AudioSource);