Search Unity

Play audio and video game objects

Discussion in 'Scripting' started by Edreyn, Aug 25, 2018.

  1. Edreyn

    Edreyn

    Joined:
    Jul 5, 2017
    Posts:
    16
    Hello all,
    Again, I apologize in advance if my question may be too silly or newbie. I did try googling before asking, and still can't manage by myself.

    In my game, I added a large number of audio and video files as GameObjects. Each of them has a pre-set audio or video source, and a specific name that is easy to remember and track in the game.

    What I need:

    1) All sounds are played at the beginning, all at once. I know I can disable them from PlayOnAwake one by one, is there a way to do it to whole directory? Not important if by code or built-in function in Unity itself.

    2) This question is more important. How can I run an audio or video file using relevant GameObject's name?

    I tried this:

    Code (CSharp):
    1.         GameObject thisObject = GameObject.Find("musTheme3");
    2.         thisObject.GetComponent<AudioSource>().Play();
    and also this:

    Code (CSharp):
    1.         GameObject Common_Sounds = new GameObject();
    2.  
    3.         Common_Sounds.SetActive(true);
    4.  
    5.         foreach (Transform thisObject in Common_Sounds.transform)
    6.         {
    7.             if (thisObject.name == "musTheme3")
    8.             {
    9.                 thisObject.gameObject.SetActive(true);
    10.                 thisObject.gameObject.GetComponent<AudioSource>().Play();
    11.                 Debug.Log(thisObject.gameObject.GetComponent<AudioSource>());
    12.             }
    13.         }
    But it seems that computer can't find relevant GameObject. ALso, I am not sure that even if he does find it, how to correctly play it?

    3) Same about videos, but it can be more complex. First, the computer must find a video GameObject by name, then play it. All videos must be played on full-screen, and only when activated.

    This is code I tried:

    Code (CSharp):
    1.         GameObject thisObject = GameObject.Find("movIntro");
    2.         VideoPlayer thisVideo = thisObject.GetComponent<VideoPlayer>();
    3.         thisVideo.Play();
    I have basic skills with generic Visual Studio, but Unity has a completely different approach.

    Thank you in advance,
    Evgenie