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

Change audiosource maxdistance by scripting with no effect

Discussion in 'Scripting' started by Georges Abitbol, Oct 30, 2013.

  1. Georges Abitbol

    Georges Abitbol

    Joined:
    Jan 28, 2013
    Posts:
    10
    I play a video on my object renderer and I play its audio clip on my object audioSource.

    When I change the audiosource maxDistance by scripting there is no impact.
    When I change the same value on the inspector the audio behaviour is good.

    Is it a bug or did I forget something ?
     
  2. cAyouMontreal

    cAyouMontreal

    Joined:
    Jun 30, 2011
    Posts:
    315
    Have you tried to change the minDistance too?
     
  3. Georges Abitbol

    Georges Abitbol

    Joined:
    Jan 28, 2013
    Posts:
    10
    After several tests, the problem does not occur on an audio clip from an audio file, only from a video file.

    Ideas?
     
  4. Georges Abitbol

    Georges Abitbol

    Joined:
    Jan 28, 2013
    Posts:
    10
    Yes. It's the same for minDistance. But volume works!
     
  5. cAyouMontreal

    cAyouMontreal

    Joined:
    Jun 30, 2011
    Posts:
    315
    Oh, you should try to get the audioClip from the video and set variables to it, if you don't do that already.
     
  6. Georges Abitbol

    Georges Abitbol

    Joined:
    Jan 28, 2013
    Posts:
    10
    Changing maxDistance on audiosource (from audio clip from audio file) works but changing maxDistance on audiosource (from audio clip from video file) does not work.
    And only by scripting.
    I don't really understand your proposition. Audioclip does not have maxDistance field.
     
  7. cAyouMontreal

    cAyouMontreal

    Joined:
    Jun 30, 2011
    Posts:
    315
    Yeah my mistake.
    If the volume is working and the distance is working only in the editor I don't really understand why you can't do it by scripting.

    I'm doing some tests.
     
  8. cAyouMontreal

    cAyouMontreal

    Joined:
    Jun 30, 2011
    Posts:
    315
    I did some tests, and it seems that the values are updated, because you can see the changes on the audio source in real time.
    But it doesn't seem to be effective, until you change a bit the value in the editor.

    Weird...
     
  9. cAyouMontreal

    cAyouMontreal

    Joined:
    Jun 30, 2011
    Posts:
    315
    Nevermind, it seems working, for me at least.
    I've put a Movie Texture on my scene, a script on it (MovieTextureTests), and an AudioSource.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MovieTextureTests : MonoBehaviour {
    6.    
    7.     public float minDistance = 10, maxDistance = 500;
    8.     private MovieTexture movieTexture;
    9.     // Use this for initialization
    10.     void Start () {
    11.         movieTexture = renderer.material.mainTexture as MovieTexture;
    12.         movieTexture.Play();
    13.         audio.clip = movieTexture.audioClip;
    14.         audio.Play();
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.         audio.minDistance = minDistance;
    20.         audio.maxDistance = maxDistance;
    21.     }
    22. }
    23.  
    Changing these values (minDistance and maxDistance) will affect the properties of the AudioSource in real time.
     
  10. Georges Abitbol

    Georges Abitbol

    Joined:
    Jan 28, 2013
    Posts:
    10
    Thanks for your test and your time.
    Indeed, the AudioSource values ​​are changed in real time.
    But you can always hear the sound whatever the distance from the listener is.
    It's not the case when you change AudioSource values directly in the inspector.

    Just after the loading, if you change minDistance or maxDistance on your MovieTextureTests component the AudioSource corresponding values change too. But there is no impact on sound.
    After that, if you set an AudioSource value in the Inspector, immediatly you can hear the impact on the sound.
    And after that, if you change minDistance or maxDistance on your MovieTextureTests component the AudioSource corresponding values change again and there is impact on sound.

    It is as if the inspector had put the AudioSource in the correct mode!
     
    Last edited: Oct 31, 2013
  11. bracejack

    bracejack

    Joined:
    Dec 9, 2013
    Posts:
    1
    I have the exact same behaviour in my environment, did you find a fix for this? I have other audio sources that work fine, but the one that is part of a video doesn't work properly. It is always the same volume no matter how close or far I am from it once it has started, and no matter what I set the volume, max distance and min distance values to, whether in the inspector or in the script. But they do work correctly once I edit one of them, or anything to do with the audio file in the inspector. Also, minimising and maximising Unity starts the audio behaving correctly, as does pausing/unpausing. It also occurs in the build version and again can be fixed by minimising/maximising. I've tried numerous things to fix it so far with no luck. It seems like a bug, but if someone thinks I may be doing something wrong somewhere I would like to know. Otherwise maybe I can find a way to take the game out and in of focus without fulling minimising/maximising whenever the video starts... Thanks in advance!
     
  12. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,516
    It sounds to me like it's being treated as a 2D sound.
     
  13. Georges Abitbol

    Georges Abitbol

    Joined:
    Jan 28, 2013
    Posts:
    10
    I still have the same problem. Without any fix, I filed a bug to Unity team on the subject. I expect an answer.