Search Unity

is PlayOneShot exist in c# unity 5?

Discussion in 'Audio & Video' started by bigroger, May 15, 2015.

  1. bigroger

    bigroger

    Joined:
    Jan 13, 2014
    Posts:
    7
    Hi,
    first of all i'm french (so please excuse my bad english) and i'm new to Unity and c# (so please excuse my bad practice) ;)

    I work on a small 2d shooter and i want my playerscript to play some sound when shooting so wrote something like :

    public class ScriptJoueur : MonoBehaviour {

    using UnityEngine;
    using System.Collections;

    public AudioClip sonTir;

    void Update () {

    if (Input.GetKey(KeyCode.UpArrow))
    {
    gameObject.transform.Translate(0, 0.1f, 0);
    }

    else if (Input.GetKey(KeyCode.DownArrow))
    {
    gameObject.transform.Translate(0, -0.1f, 0);
    }

    if (Input.GetKey(KeyCode.LeftArrow))
    {
    gameObject.transform.Translate(-0.1f, 0, 0);
    }
    else if (Input.GetKey(KeyCode.RightArrow))
    {
    gameObject.transform.Translate(0.1f, 0, 0);
    }



    if (Input.GetKeyDown(KeyCode.Space))
    {
    gameObject.audio.PlayOneShot(sonTir);
    }

    }
    }

    ...and VisualStudio insult me like this

    Error 1 'UnityEngine.Component' does not contain a definition for 'PlayOneShot' and no extension method 'PlayOneShot' accepting a first argument of type 'UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)

    I really don't understand...
     
  2. Gametyme

    Gametyme

    Joined:
    May 7, 2014
    Posts:
    618
    You didn't declare your AudioSource var and you didn't get its reference.
    Code (CSharp):
    1. Public AudioClip sound;
    2. AudioSource audio;
    3.  
    4. void Start()
    5. {
    6.      audio = GetComponent<AudioSource>();
    7. }
    8.  
    9. Void Update()
    10. {
    11.     if(input blah)
    12.         audio.PlayOneShot(sound, 0.8);
    13. }
     
    Mohamoha200 likes this.
  3. bigroger

    bigroger

    Joined:
    Jan 13, 2014
    Posts:
    7
    ok thank you verry much !

    just a little thing... is this new for unity 5?
    I mean the code i wrote is a tutorial for unity 4.5.3
     
  4. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    Forced caching is new, yes.

    Code (csharp):
    1. audio = GetComponent<AudioSource>();
    Although people should have been doing that anyway. I just add audiosources to objects in script, not via the inspector. It's cleaner.
     
    Last edited: May 15, 2015
  5. bigroger

    bigroger

    Joined:
    Jan 13, 2014
    Posts:
    7
    okay thanx ...

    actually in
    1. audio = GetComponent<AudioSource>();
    is GetComponent is the way to access the method PlayOneShot() of the AudioSource class?
    I hope my question is not too stupid... ;p

    GetComponent is not a very easy "concept" for a beginner
     
  6. bigroger

    bigroger

    Joined:
    Jan 13, 2014
    Posts:
    7
    actually i don't really understand in english what you mean by "get its reference" ... do you mean access property like the get keyword could do?
     
  7. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    So, let's say you have attached an audiosource to your game object in the inspector. That Audiosource is now a "component" of that game object.

    Let's say you also attach a script to that game object. That script is also a component of that game object.

    Now you want to access the audiosource that is attached to the game object from your script that is also attached to the game object... but your script doesn't know that the audiosource is there until you tell it that it's there.

    So you have to declare an AudioSource.

    Code (csharp):
    1. Audiosource MyReferenceToTheAudioSource;
    2.  
    and then you have to fetch a reference to that particular audiosource attached to your game object. You do that by asking the game object to find a component that matches your criteria and return the reference.

    Code (csharp):
    1.  
    2. Void Start(){
    3. MyReferenceToTheAudioSource = gameObject.GetComponent<AudioSource>();
    4. }
    5.  
    Here we are just telling the game object the script is attached to, to find the first component that is attached to it that is of type AudioSource.

    The AudioSource we declared in our script now points to the AudioSource component attached to iur game object.

    Now your script can finally communicate with the AudioSource component via the reference MyReferenceToTheAudioSource. The same method is used for scripts to be able to communicate with other types of components as well, including other scripts.

    PlayOneShot is just a function of the AudioSource attached to our game object, but we are not able to access it until we link the script to that component.
     
    Last edited: May 16, 2015
  8. bigroger

    bigroger

    Joined:
    Jan 13, 2014
    Posts:
    7
    thanks a lot for your help i think i understood (finally !!)