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

The documentation is kinda half...

Discussion in 'Scripting' started by BIO_K, Jun 23, 2022.

  1. BIO_K

    BIO_K

    Joined:
    Feb 3, 2016
    Posts:
    23
    https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html I have been using the below script for music playing the problem isx come at scene 3 I wanna change the music but than I have two songs playing... How to make it work? So since its only called on awake I suppose its only in the main menu scene, and never again even in a new scene?

    Structure of My Game -
    Scene 1: Main Menu - same song ()DontDestroyOnLoad)
    Scene 2: Char Select - same song
    Scene 3: GamePlay - Different Song the destroy the other one...

    You would think:
    if (objs.Length > 1)
    {
    Destroy(this.gameObject);
    }
    is the solution hey...
     
  2. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    I don't understand, what are you trying to do?
     
    Bunny83 likes this.
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    This post is 'kinda half', whatever that means.

    The documentation code samples are very simple examples of how a method can be used. You can't expect to copy and past them into your own project and have them work.
     
    Bunny83, bplc, passerbycmc and 2 others like this.
  4. BIO_K

    BIO_K

    Joined:
    Feb 3, 2016
    Posts:
    23
    I fixed it... Have to have two scripts now which kinda sucks... Trust me I never copy paste code... I can use an example though...
     
  5. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    I'm happy for you, but I'm curious, what did you want to achieve? You know it's not very serious to have several scripts or even to copy and paste code, the main thing is to know what we are doing ^^
     
  6. BIO_K

    BIO_K

    Joined:
    Feb 3, 2016
    Posts:
    23
    Structure of My Game -
    Scene 1: Main Menu - same song ()DontDestroyOnLoad)
    Scene 2: Char Select - same song
    Scene 3: GamePlay - Different Song the destroy the other one...

    i wanted to achieve this... I know what I am doing mostimes... I did'nt make Unity, so knowing when the Awake function on a DontDestroyOnLoad gameobject is called can be confusing as it could be either once or everytime the scene changes.... I never use this function... But its the only way to keep the music pumpin... I use the Binary formatter for those kinda things...
     
  7. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    Ok if I understood correctly your problem was to have the same music in your main menu, as well as in the selection menu of your champion, and another music in game, but you cannot manage to configure your code, therefore, in game, you ended up with the 2 previous active musics as well as the new one, is that right? I therefore invite you to look at the audioSource API a little more carefully, there are many things that could be useful to you.
     
  8. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    Awake 
    means that the code will be executed before the code
    void Start() {}
     
    Last edited: Jun 23, 2022
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    Personally I'd avoid DontDestroyOnLoad and go instead for additive scene loading.

    You could easily make a music manage object that hangs around in an ever present scene, with global methods for changing the current song.
     
    bplc likes this.
  10. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    Ok I understood, he would like to know how to create a condition that says :

    if (Scene3)
    does not play
    public static void DontDestroyOnLoad ( Object target );

    Because he used the DontDestroyOnLoad method, to synchronize his first music, when going from scene 1 to scene 2.

    Why make it simple when you can make it complicated xDDD
     
  11. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    If he absolutely wants to keep his system in place, he could do:

    Code (CSharp):
    1. public class MyScript: MonoBehaviour
    2. {
    3.  
    4.     AudioSource audioSource;
    5.  
    6.     void Awake()
    7.     {
    8.  audioSource = GetComponent<AudioSource>();
    9.  GetComponent<AudioSource>().Stop ();
    10.     }
    11. }
    12.  
    13.    

    On a script on scene 3 ??



    But I agree with spiney199, I wouldn't have proceeded like that, but I still find it interesting :)


    After for the synchronization method, have you tried with audioSource.time or AudioSource .timeSamples, for recover the time values of your music to transfer them to the next scene.
     
    Last edited: Jun 25, 2022