Stop AudioSource on Trigger!!!
Hello.
I am having trouble getting this script to work.
What i want is: I have a sprite with the listed script below attached to it, when my sprite enters the "Ground [UNREACHABLE]" trigger collider, i want the AudioSource attached to my camera to stop playing. There is only 1 AudioSource on the camera.
It's pretty basic and simple, but i am still new to coding, so i am really struggling!
I appriciate all help i can get!
Script (c#):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FreezeTime : MonoBehaviour {
public AudioSource backgroundM;
public float slowdownFactor = 1f;
public float slowdownLength = 10f;
void Start()
{
Camera.main.gameObject.GetComponents<AudioSource>();
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "Ground [UNREACHABLE]")
{
Time.timeScale = slowdownFactor;
Debug.Log("Insert code here");
}
}
}
Dear Cedric, I CONFIR$$anonymous$$ YOUR $$anonymous$$ESSAGE But couldn't help you. I'm Economics who just learn to develop in different language. As well it would be risky yet to bring you correction in your encoding phrase
Ok... I appricate you saying that, but there was no need! If you don't know how to help me, then why bother making this comment in the first place?
Answer by Vega4Life · Aug 09, 2019 at 09:30 PM
Pretty straightforward. Grab the source reference from the camera, then call the stop function on the reference.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FreezeTime : MonoBehaviour
{
public AudioSource backgroundM;
public float slowdownFactor = 1f;
public float slowdownLength = 10f;
AudioSource cameraAudioSource;
void Start()
{
cameraAudioSource = Camera.main.gameObject.GetComponent<AudioSource>();
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "Ground [UNREACHABLE]")
{
Time.timeScale = slowdownFactor;
cameraAudioSource.Stop();
}
}
}
Oh sorry! I am an idiot! What i actually meant is: The script is attached to a sprite, and when that sprite enters another sprite´s trigger collider, the AudioSource on the camera gets stopped. $$anonymous$$y bad! I already updated the question!