Search Unity

Question Audio Source dissapears when running play mode

Discussion in 'Editor & General Support' started by mambokamboluc, Nov 25, 2021.

  1. mambokamboluc

    mambokamboluc

    Joined:
    Nov 25, 2021
    Posts:
    2
    When I try to run my game the Audio Source just dissapears while the GameObject is given before i start it.
    I'm using Unity version 2020.3.4f1

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.UI;
    7.  
    8. public class MainMenu : MonoBehaviour
    9. {
    10.     public GameObject mainMenuUI;
    11.     public GameObject settingsMenuUI;
    12.     public GameObject soundsMenuUI;
    13.     public Slider mainVolumeSlider;
    14.     public Text mainVolumeText;
    15.     public float volumeValue = 1f;
    16.     public AudioSource OpeningSource;
    17.  
    18.     // Update is called once per frame
    19.     void Start()
    20.     {
    21.         OpeningSource = GetComponent<AudioSource>();
    22.     }
    23.     public void Settings()
    24.     {
    25.         mainMenuUI.SetActive(false);
    26.         settingsMenuUI.SetActive(true);
    27.     }
    28.     public void Sounds()
    29.     {
    30.         settingsMenuUI.SetActive(false);
    31.         soundsMenuUI.SetActive(true);
    32.     }
    33.     public void Back()
    34.     {
    35.         settingsMenuUI.SetActive(false);
    36.         mainMenuUI.SetActive(true);
    37.     }
    38.     public void MainChangeValue()
    39.     {
    40.         Debug.Log(mainVolumeSlider.value);
    41.         volumeValue = mainVolumeSlider.value / 100;
    42.         mainVolumeText.text = (volumeValue * 100).ToString() + "%";
    43.         OpeningSource.volume = volumeValue;
    44.     }
    45.  
    46. }
    47.  
    48.  
    49.  
    Screenshot 2021-11-26 004018.png
    Screenshot 2021-11-26 004106.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Line 21 contradicts how you are setting it up.

    Either set it up in the editor as you show above, OR use GetComponent<T>() on the correct GameObject to find it.

    Do not do both.
     
  3. mambokamboluc

    mambokamboluc

    Joined:
    Nov 25, 2021
    Posts:
    2
    Thank you for your help it works again. :)
     
    Kurt-Dekker likes this.