So hey. I have an Audio Source component attached to my object. I also copied the script from here and attached it to my object.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class ExampleClass : MonoBehaviour
{
public AudioClip impact;
AudioSource audio;
void Start()
{
audio = GetComponent<AudioSource>();
}
public void play_sound()
{
audio.PlayOneShot(impact, 0.7F);
}
}
but when I try to use audio in my script I get it cut
what is the problem?
You are inhering from MonoBehaviour
which inherits from Behaviour
and then from Component
which has some variables declared in them such as:
public Component animation { get; }
public Component audio { get; }
public Component camera { get; }
public Component collider { get; }
public Component collider2D { get; }
public Component constantForce { get; }
public Component guiElement { get; }
public Component guiText { get; }
public Component guiTexture { get; }
public Component hingeJoint { get; }
public Component light { get; }
public Component networkView { get; }
public Component particleEmitter { get;
public Component particleSystem { get; }
public Component renderer { get; }
public Component rigidbody { get; }
public Component rigidbody2D { get; }
These are all deprecated and no longer allowed to be used in the latest version of Unity. I believe it was deprecated from Unity 5 upwards. The document you linked is not yet updated. For more information see this post.
All you have to do is rename your audio variable to something else and everything should work fine.
public AudioClip impact;
AudioSource myAudio;
void Start()
{
myAudio = GetComponent<AudioSource>();
}
public void play_sound()
{
myAudio.PlayOneShot(impact, 0.7F);
}