Audio Clip Error.

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour 
{
    public GameManager manager;
    public float moveSpeed;
    public GameObject deathParticles;

    private float maxSpeed = 5f;
    private Vector3 input;

    private Vector3 spawn;

    public AudioClip[] audioClip;

    public Rigidbody myRigidbody { get; private set; }

	// Use this for initialization
	void Start () 
    {
        spawn = transform.position;
        manager = manager.GetComponent<GameManager>();

        this.myRigidbody = this.GetComponent<Rigidbody>();
	}
	
	void FixedUpdate () 
    {
	    input = new Vector3(Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
        if (myRigidbody.velocity.magnitude < maxSpeed)
        {
            myRigidbody.AddRelativeForce(input * moveSpeed);
        }

        if (transform.position.y < -2)
        {
            Die();
        }
	}

    void OnCollisionEnter(Collision Other)
    {
        if (Other.transform.tag == "Enemy")
        {
            Die();
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.transform.tag == "Enemy")
        {
            Die();
        }

        if (other.transform.tag == "Token")
        {
            manager.tokenCount += 1;
            PlaySound(0);
            Destroy(other.gameObject);
        }

        if (other.transform.tag == "Goal")
        {
            manager.CompleteLevel();
            PlaySound(1);
        }
    }

    void PlaySound(int clip)
    {
        audio.clip = audioClip[clip];
        audio.Play();
    }
    
    void Die()
    {
            Instantiate(deathParticles, transform.position, Quaternion.Euler(270, 0, 0));
            transform.position = spawn;
    }
}

The Code Problem

void PlaySound(int clip)
{
audio.clip = audioClip[clip];
audio.Play();
}
“audio.clip” and “audio.Play();”

Unity Error;

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

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

Help me Pls :frowning:

Error 1 is a simple type casting issue. A lot of the old access properties (like audio, rigidbody, colider, etc) are no longer stored as their type, but as their base type “Component” this I believe was related to unity making their code base a bit more modular.

This is also why these properties are marked as “Deprecated” and should be phased out of use. I would advise using GetComponent to get the AudioSource component. like the following example.

private AudioSource audioSource;

void Awake()
{
	audioSource = GetComponent<AudioSource>();
}

Then play your audio through “audioSource” instead of “audio”

Error 2 is the same issue, since “audio” is type “Component” it does not have a method “Play” but when you swap to using an explicitly typed variable for your audioSource this should not be a problem.

This logic is not supporting , please say what to do