Skip to content

Instantly share code, notes, and snippets.

@OutlawGameTools
Created February 11, 2015 22:13
Show Gist options
  • Save OutlawGameTools/189406d7cc8316c5c784 to your computer and use it in GitHub Desktop.
Save OutlawGameTools/189406d7cc8316c5c784 to your computer and use it in GitHub Desktop.
Unity: Attach to game objects that can be picked up (collider2d, isTrigger). Play audio if it exists on game object.
#pragma strict
public var pickupValue : int = 5;
function OnTriggerEnter2D(other : Collider2D)
{
var gObj : GameObject = other.gameObject;
var clip : AudioClip;
if (gObj.CompareTag("Player"))
{
// Add pickupValue to total treasure amount
if (gameObject.audio)
{
clip = gameObject.audio.clip;
AudioSource.PlayClipAtPoint(clip, new Vector3 (5, 1, 2));
}
Debug.Log("Grabbed treasure! " + gameObject.name + " " + pickupValue + " points!" );
Destroy(gameObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment