- Home /
Not able to play a sound using 'PlayOneShot' in C# script in Unity3D
Hi,
I was trying to build a C# code from Java Script using 'm2h Converter' for playing a sound in unity using 'Play.OneShot'. Java Script for this code is here:
var clip : AudioClip;
function Start() {
audio.PlayOneShot(clip);
}
Now, Can anyone help me to convert this Java Script into C# script. I've converted this but it is giving me an error.
using UnityEngine; using System.Collections;
public class AudioClip : MonoBehaviour {
public AudioClip clip;
void Start ()
{
audio.PlayOneShot(clip);
}
}
error : In line 'audio.PlayOneShot(clip);' -> The best overload method match for 'UnityEngine.AudioSource.PlayOneShot(Unity.Engine.AudioClip)'
Answer by fafase · Jan 24, 2013 at 07:42 AM
Your class has the same name as the variable type. You are probably overwriting the type inside the assembly. That means your:
public AudioClip clip;
is a variable of the type of the class you are working on and not a AudioClip from MonoBehaviour. Hence the function is receiving a variable of the wrong type.
Change your class name for something that does not already exist like AudioTestClass or else.
Thank you for your help.
Its works!!
I've changed the name of my class and it build successfully.
Thanks once again!!
Answer by cdrandin · Jan 24, 2013 at 06:55 AM
Try http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.PlayOneShot.html
That alone should have worked.
Your answer
Follow this Question
Related Questions
Implementing binaural recording in an interactive manner -1 Answers
No sound in my video after capturing my screen? 0 Answers
Jumping sound not working after respawning 1 Answer
Get Audio from Speakers 0 Answers