Hi everybody !
I’m really new to scripting and i’m in encounter a problem that make me crazy !
I’ve a multiplpe trigger zone when you entering them, an audioclip will be played (link the audio to the inspector).
I’ve this :
using UnityEngine;
var audioSource : AudioSource;
public class AudioTrigger : MonoBehaviour
{
public float fadeOutFactor = 0.1f;
float audioVolume;
void OnTriggerEnter(Collider other)
{
audio.Play();
}
void OnTriggerExit()
{
audio.Stop();
}
public void fadeIn()
{
if (audio < 1)
{
audio += 0.1 * Time.deltaTime;
audio.volume = audio2Volume;
}
}
public void fadeOut()
{
if(audio > 0.1)
{
audio -= 0.1 * Time.deltaTime;
audio.volume = audio1Volume;
}
}
}
But this doesn’t work…
Can you explain me why and how to repair this ?
Thanks 
I haven’t done much myself with audio yet, but seeing your code you’re missing out on one thing. You’re increasing and decreasing a value called “audio”. I don’t know what that will do, but in your case you need to use audio.volume (at least, I reckon). Add and substract the 0.1* deltatime to/from that. Also, don’t forget the 0.1 needs to be a float!
Edit: Shouldn’t you be getting errors?
The audioSource isn’t defined correctly assuming you’re using c#. You’re also not referencing to the actual audiosource itself.
using UnityEngine;
private AudioSource audio;
public class AudioTrigger : MonoBehaviour
{
//These 2 fields aren't used anymore now
public float fadeOutFactor = 0.1f;
float audioVolume;
void OnTriggerEnter(Collider other)
{
audio.Play();
}
void OnTriggerExit()
{
audio.Stop();
}
public void fadeIn()
{
if (audio.volume < 1)
{
audio.volume += 0.1f * Time.deltaTime;
}
}
public void fadeOut()
{
if (audio.volume > 0.1)
{
audio.volume -= 0.1f * Time.deltaTime;
}
}
}
Hello, this is a simple Audio fade in / fade out script, with trigger function.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class ??????????? : MonoBehaviour {
public AudioSource audio;
public float fadeOutFactor = 0.08f;
public float fadeInFactor = 0.08f;
private bool fadeInOut = false;
void Start()
{
audio.volume = 0.0f;
}
void Update()
{
if (audio.volume <= 0.0f) { audio.Stop(); }
if (audio.volume >= 1.0f) { audio.Play(); }
if (fadeInOut == true)
{
if (audio.volume < 1.0f)
{
audio.volume += fadeInFactor * Time.deltaTime;
}
}
if (fadeInOut == false)
{
if (audio.volume > 0.0f)
{
audio.volume -= fadeOutFactor * Time.deltaTime;
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
fadeInOut = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
fadeInOut = false;
}
}
}