Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

OnMouseOver fadeMusic [help(included video)]

Discussion in 'Scripting' started by RPP, Dec 4, 2015.

  1. RPP

    RPP

    Joined:
    Nov 11, 2015
    Posts:
    28
    Hello again, so today i decided to work with an Audio Source and Audio Clip. I created an introduction song in FLStudio and saved it as an .ogg file extension. The song loads fine and i have it loaded in an "Empty Object" on the scene, named, "AudioLoadScreen". I correctly linked the Audio source however, I am having problems trying to load an Audio Clip in my code. If any one could steer me in the right direction i would really appreciate it! I attached this script to the 2 objects i want to have the volume fade when moused over.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class fadeMusicOnMouseOver : MonoBehaviour {
    5.    
    6.     public AudioSource mySong;
    7.     public AudioClip selectedSong;
    8.  
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.         mySong = GameObject.Find("AudioLoadScreen").GetComponent<AudioSource>();
    13.  
    14.         selectedSong = GameObject.Find("AudioLoadScreen").GetComponent<AudioClip>();
    15.        //is wrong but allows my code to compile.
    16.  
    17.  
    18.  
    19.  
    20.  
    21.  
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update ()
    26.     {
    27.  
    28.     }
    29.  
    30.     void OnMouseOver()
    31.     {
    32.         mySong.volume -=.02f;
    33.     }
    34.  
    35.     void OnMouseExit()
    36.     {
    37.         if(mySong.volume < 1)
    38.         {
    39.             mySong.volume +=.02f;
    40.         }
    41.     }
    42.  
    43. }
     
  2. LukePammant

    LukePammant

    Joined:
    Mar 10, 2015
    Posts:
    50
    Since your mySong and selectedSong variables are public why not just drag the audio source and audio clip on them from the inspector? Make sure your script is assigned to a game object and simple drag the AudioLoadScreen into the slots?
     
  3. RPP

    RPP

    Joined:
    Nov 11, 2015
    Posts:
    28
    Well, i prefer to load my objects through code so they are easily applied and more direct. However, i loaded both my objects into the variables through the inspector and my scroll over and exit methods still do not work, i assume i need to edit my code. Its my first time using OnMouseOver() functions, i don't know if I am using them properly.

    Heres my new code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class fadeMusicOnMouseOver : MonoBehaviour {
    5.    
    6.    public AudioSource mySong;
    7.    
    8.    private bool increaseVolume, decreaseVolume;
    9.    
    10.    // Use this for initialization
    11.    void Start ()
    12.    {
    13.      mySong.Play();
    14.      increaseVolume = false;
    15.      decreaseVolume = false;
    16.    }
    17.    
    18.    // Update is called once per frame
    19.    void Update ()
    20.    {
    21.  
    22.      if(decreaseVolume == true && increaseVolume == false)
    23.      {
    24.        Decrease();
    25.      }
    26.  
    27.      else
    28.      {
    29.        if(decreaseVolume == false && increaseVolume == true)
    30.        {
    31.          Increase();
    32.        }
    33.      }
    34.  
    35.    }
    36.  
    37.    void OnMouseOver()
    38.    {
    39.      decreaseVolume = true;
    40.      increaseVolume = false;
    41.    }
    42.    
    43.    void OnMouseExit()
    44.    {
    45.      increaseVolume = true;
    46.      decreaseVolume = false;
    47.    }
    48.    
    49.    void Decrease()
    50.    {
    51.      mySong.volume -=.02f;
    52.    }
    53.  
    54.    void Increase()
    55.    {
    56.      if(mySong.volume < 1)
    57.      {
    58.        mySong.volume +=.02f;
    59.      }
    60.    }
    61.    
    62. }
    63.  
     
    Last edited: Dec 6, 2015
  4. Mich_9

    Mich_9

    Joined:
    Oct 22, 2014
    Posts:
    118
    Tell to the AudioSource to start playing first, and there is no need for the AudioClip variable, just assign the AudioClip to the AudioSource component in the inspector. And also use Time.deltaTime to change the volume so it will be framerate independent.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class fadeMusicOnMouseOver : MonoBehaviour {
    6.     public AudioSource mySong;
    7.     public float VolumeSpeed = 1;
    8.     private bool increaseVolume = false;
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.          mySong.Play();
    13.     }
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.         if(increaseVolume){
    18.             Increase();
    19.         } else {
    20.             Decrease();
    21.         }
    22.     }
    23.    
    24.     void OnMouseOver()
    25.     {
    26.         increaseVolume = true;
    27.     }
    28.     void OnMouseExit()
    29.     {
    30.         increaseVolume = false;
    31.     }
    32.    
    33.     void Decrease()
    34.     {
    35.         mySong.volume -= VolumeSpeed * Time.deltaTime;
    36.     }
    37.     void Increase()
    38.     {
    39.         mySong.volume += VolumeSpeed * Time.deltaTime;
    40.     }
    41. }
    42.  
     

    Attached Files:

  5. RPP

    RPP

    Joined:
    Nov 11, 2015
    Posts:
    28
    Okay, so i did as follows, and currently when i run the scene the volume begins to decrease immediately, rather than waiting when i hover over the object. I attached the script to other objects to see if it was my object causing the problem but that seems not to be the case. I imagine the OnMouseOver script becomes activated as soon as the scene starts some reason. Do i need to declare the object within OnMouseOver()? I'll see if i can post a video of what is happening to have more closure.
     
    Last edited: Dec 6, 2015
  6. RPP

    RPP

    Joined:
    Nov 11, 2015
    Posts:
    28
    Okay, SO here is the script and video. In the video you can see the music plays and begins to fade out as soon as the scene starts. Rather then when i want to have it fade when i mouse over the "stage 1" object that its linked too. Thanks for all the help everyone, it would be awesome if i could get this figured out!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class fadeMusicOnMouseOver : MonoBehaviour
    5. {
    6.    public AudioSource mySong;
    7.  
    8.    public float VolumeSpeed = .2f;
    9.  
    10.    private bool increaseVolume = true;
    11.  
    12.    // Use this for initialization
    13.    void Start()
    14.    {
    15.      mySong.Play();
    16.    }
    17.    // Update is called once per frame
    18.    void Update()
    19.    {
    20.      if(increaseVolume)
    21.      {
    22.        Increase();
    23.      }
    24.  
    25.      else
    26.      {
    27.        Decrease();
    28.      }
    29.    }
    30.    
    31.    void OnMouseOver()
    32.    {
    33.      increaseVolume = false;
    34.    }
    35.    void OnMouseExit()
    36.    {
    37.      increaseVolume = true;
    38.    }
    39.    
    40.    void Decrease()
    41.    {
    42.      mySong.volume -= VolumeSpeed * Time.deltaTime;
    43.    }
    44.    void Increase()
    45.    {
    46.      mySong.volume += VolumeSpeed * Time.deltaTime;
    47.    }
    48. }
    49. }

    Here is the video:
     
    Last edited: Dec 7, 2015
  7. Mich_9

    Mich_9

    Joined:
    Oct 22, 2014
    Posts:
    118
    The volume music start decreasing because the initial value of increaseVolume:
    Code (csharp):
    1. private bool increaseVolume = false;
    Change it to true.
     
  8. RPP

    RPP

    Joined:
    Nov 11, 2015
    Posts:
    28
    Okay i have set to true, and now the music does not fade at all, i have no idea why this simple code wont run properly? I added the box collider, after reading that OnMouseOver requires a collider component, but i hardly think that's the area its going wrong?
     
    Last edited: Dec 7, 2015
  9. Mich_9

    Mich_9

    Joined:
    Oct 22, 2014
    Posts:
    118
    I tested it and is working for me. One more attempt:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class fadeMusicOnMouseOver : MonoBehaviour
    5. {
    6.     public AudioSource mySong;
    7.     public float VolumeSpeed = 1;
    8.     private bool increaseVolume = true;//Changed from false to true
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.         mySong.Play();
    13.     }
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if (increaseVolume)
    18.         {
    19.             Increase();
    20.         }
    21.         else
    22.         {
    23.             Decrease();
    24.         }
    25.     }
    26.  
    27.     void OnMouseOver()
    28.     {
    29.         increaseVolume = true;
    30.     }
    31.     void OnMouseExit()
    32.     {
    33.         increaseVolume = false;
    34.     }
    35.  
    36.     void Decrease()
    37.     {
    38.         mySong.volume -= VolumeSpeed * Time.deltaTime;
    39.     }
    40.     void Increase()
    41.     {
    42.         mySong.volume += VolumeSpeed * Time.deltaTime;
    43.     }
    44. }
     
  10. RPP

    RPP

    Joined:
    Nov 11, 2015
    Posts:
    28
    Sorry to bring this question back up, but i did what most people would do and ignore that task and move onto another part of my program. However, I have now finished that part and cant move forward till I get this dang music to fade OnMouseOver.... I thank all those that have tried helping, if anyone could give any form of input that would be awesome! The video posted above should help you understand in more detail my problem. Thanks again!