Skip to content

Instantly share code, notes, and snippets.

@SenpaiRar
Created May 14, 2019 15:19
Show Gist options
  • Save SenpaiRar/97acb161e874d8e84e37635cd29f0849 to your computer and use it in GitHub Desktop.
Save SenpaiRar/97acb161e874d8e84e37635cd29f0849 to your computer and use it in GitHub Desktop.
Food items use the method I described above for audio because they're destroyed immediately as their clicked; we need to use on external audio source.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class FoodScript : ClickableParent {
public AudioSource Source;
public AudioClip Clip;
void Start () {
//when the gameobject is created, we find the audiosource on the player so we can use that source
Source = GameObject.Find("Player").GetComponent<AudioSource>();
}
public override void OnClick(Vector3 Clickposition)
{
Debug.Log("Clicked " + Name + ", Distance: " + Vector3.Distance(Clickposition, transform.position));
if(Vector3.Distance(Clickposition, transform.position) <= MMinimumDistance){
Master_Manager.current_Hunger_Level += food_Value;
//Because we're calling Destroy right after playing the audio, we need to use an audiosource on on the gameobject
//Otherwise, the audio would be interrupted when its destroyed
Source.PlayOneShot(Clip);
Destroy(gameObject);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment