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)’

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.

Try Unity - Scripting API: AudioSource.PlayOneShot

That alone should have worked.