I trying to play a sound while the player is ontriggerenter. The sound is linked to the object with a AudioSource component. But notting happened.
using UnityEngine;
using System;
using System.Collections;
public class FloatBehavior : MonoBehaviour {
Vector2 floatY;
float originalY;
public float floatStrength = 1;
AudioSource triggerAudio; // Reference to the AudioSource component.
void Start () {
this.originalY = this.transform.position.y;
triggerAudio = GetComponent <AudioSource> ();
Debug.Log (triggerAudio);
}
void Update () {
transform.position = new Vector3(transform.position.x,
originalY + ((float)Math.Sin(Time.time) * floatStrength),
transform.position.z);
}
// OnTriggerEnter 2D
void OnTriggerEnter2D(Collider2D other) {
// If gameObject comes in contact with player
if (other.gameObject.tag == "Player") {
// Play the hurt sound effect.
triggerAudio.Play ();
}
}
}
Trying to debug, but the triggerAudio
object will retrieve: woodstump 3 (UnityEngine.AudioSource) UnityEngine.Debug:Log(Object) FloatBehavior:Start() (at Assets/C#/Objects/FloatBehavior.cs:17)
.
What am I missing in my script? Should I use AudioClip
also?