So for starters, I wish to say that the code below works. But its not structured how I want it to:
public AudioClip pickup;
void OnTriggerEnter(Collider collider)
{
//Trigger for pickups
if (collider.gameObject.CompareTag("Pickup"))
{
AudioSource audio = GetComponent<AudioSource>();
audio.PlayOneShot(pickup);
}
}
But I want to split the class into two and have the player controller (part of it written above) be used to call a function from a new class called 'LevelClass' which will house the method for pickup like so:
public void collidePickup(Collider collider)
{
collider.gameObject.SetActive(false);
AudioSource audio = GetComponent<AudioSource>();
audio.PlayOneShot(pickup);
}
and have player controller call the function like so:
level.collidePickup(collider);
I also linked the sound file with the script so that it would have a sound to play on. From testing, I determined that the problem is being caused in the AudioSource section as its not finding an audio source to work with. Is there something i can do so that i would be able to use an instance of audiosource within the levelclass rather than in the playercontroller?