Play Audio Script - Volume adjusting

Recommended Posts


I am composer and sound designer that is trying to learn some codeing :) Could You help me and tell me what should i add to change volume to 1.0F when enter trigger

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioPlay : MonoBehaviour {

    public AudioClip strings_1;
    AudioSource audioSource;

    // Use this for initialization
    void Start () {

        audioSource = GetComponent<AudioSource> (); 

        audioSource.PlayOneShot (strings_1, 0.0F );   - when game starts i want it to start but with 0 volume


    // Update is called once per frame
    void Update () {


    void OnTriggerEnter2D (Collider2D other) 

        if(other.gameObject.tag == "slotviolin")

            Debug.Log ("coilder works");
        // how can i change volume to 1.0F and if it's possible how to make it smooth ?



Thanks :)

Edited by mbmusicpl

Share this post

Link to post
Share on other sites

You should look at Unity's manual.

Check out AudioSource:

On Unity's site they have a tutorials section for Audio:

I'm not 100% sure about fading as I don't use Unity, but check out this script for smooth audio transitions:

This should help solve your problem.


Edited by Rutin

Share this post

Link to post
Share on other sites

Thx Rutin,

Somehow i have missed this tutorial with the solution :). I have to use mixer snapshots !

I still have a problem with UNITY. I am making 2D game so i changed it on tutorial script:

The problem is that i am working on UI panels and colliders doesn't work normaly here. I thing I have to use EventTrigger Componet

but what next ???

using UnityEngine;
using System.Collections;
using UnityEngine.Audio;

public class InOutMusic : MonoBehaviour {

    public AudioMixerSnapshot OutSlot;
    public AudioMixerSnapshot InSlot;
    public AudioClip[] stings;
    public AudioSource stingSource;
    public float bpm = 128;

    private float m_TransitionIn;
    private float m_TransitionOut;
    private float m_QuarterNote;

    // Use this for initialization
    void Start () 
        m_QuarterNote = 60 / bpm;
        m_TransitionIn = m_QuarterNote;
        m_TransitionOut = m_QuarterNote * 32;


    void OnTriggerEnter2D (Collider2D other) - i have added 2d
        if (other.CompareTag("slotviolin"))


    void OnTriggerExit2D(Collider2D other) - i have added 2d
        if (other.CompareTag("slotviolin"))




Edited by mbmusicpl

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By arash khalaqhdoust
      hey guys i hope you doing all well. last night i released my first game in google app store, i really appreciate you guys  to download it. and share your reviews about it
      the idea of game comes from mini hackgame of Bioshock.
       link of download:
      many thanks
    • By ForgedInteractive
      Who We Are
      We are Forged Interactive, a small team of like-minded game developers with the sole purpose of making games we love! Currently, we're progressing very quickly with our first project and there are plenty of opportunities and work for new interested programmers. With this project, our development platform is Unity 5.5.2 and C# as our behavioral language. Since this project is our first release, the game itself is a smaller project though progress is moving quickly. We are looking to finalize the current project and get started on future projects in the near future and are expanding our team to do so.
      Who We Are Looking For:
      Programmer Level Designer  
      About the Game
      Ours is the tale of two siblings, thrown into a world of chaos. Living in the shadow of their parents' heroic deeds and their Uncle's colorful military career, Finn and Atia are about to become the next force to shape our world. How will you rise through the ranks of Hereilla and what will be your legacy? Once defeated your enemies turn coat and join you in your adventures. Players can enjoy a range of troops and abilities based on their gameplay style which become more important as maps introduce more challenging terrain, enemies and bosses. Strong orc knights, dangerous shamans, and even a dragon are out on the prowl. Knowing when to fight and when to run, and how to manage your army is essential. Your actions alone decide the fate of this world.
      Previous Work by Team
      Although we are working towards our first game as Forged Interactive, our team members themselves have worked on titles including and not limited to:
      Final Fantasy Kingsglaive FIFA 2017 Xcom 2 Civilization  
      What do we expect?
      Reference work or portfolio. Examples what have you already done and what projects you have worked on academic or otherwise. The ability to commit to the project on a regular basis. If you are going on a two-week trip, we don't mind, but it would be good if you could commit 10+ hours to the project each week. Willingness to work with a royalty based compensation model, you will be paid when the game launches. Openness to learning new tools and techniques
      What can we offer?
      Continuous support and availability from our side. You have the ability to give design input, and creative say in the development of the game. Shown in credits on websites, in-game and more. Insight and contacts from within the Industry.
      If you are interested in knowing more or joining, please email or PM us on Skype. A member of our management team will reply to you within 48 hours.
      Skype: ForgedInteractive
      David, Colin and Joseph
      Follow us on:
      Twitter: @ForgedInteract

  • Popular Now