Play random video within prefab list

Single video player

using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.Video;
using UnityEngine;

public class PlayVideo : MonoBehaviour
{
    public GameObject videoPlayer;

    void Start()
    {
        videoPlayer.SetActive(false);
    }

    void OnTriggerEnter(Collider player)
    {
        if (player.gameObject.tag == "Player01")
        {
            videoPlayer.SetActive(true);
        }
    }

}

Random video player within prefablist

using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.Video;
using UnityEngine;

public class PlayVideo : MonoBehaviour
{
    List<GameObject> prefabList = new List<GameObject>();
    public GameObject videoPlayer01;
    public GameObject videoPlayer02;
    public GameObject videoPlayer03;

    void Start()
    {
        videoPlayer01.SetActive(false);
        videoPlayer02.SetActive(false);
        videoPlayer03.SetActive(false);
    }


    void OnTriggerEnter(Collider player)
    {
        if (player.gameObject.tag == "Player01")
        {
            //videoPlayer01.SetActive(true);
            int prefabIndex = UnityEngine.Random.Range(0,       );
         Instantiate(prefabList[prefabIndex]);
        }   
    }
}

It’s trivial I guess but I can’t solve this true/false variable “videoPlayerXX.SetActive(true);”
Please help, Thank you!!!

As bizarre as it looks this seems to be working

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine.SceneManagement;
    using UnityEngine.Video;
    using UnityEngine;
   
    public class PlayVideo : MonoBehaviour
    {
        List<GameObject> prefabList = new List<GameObject>();
        public GameObject videoPlayer01;
        public GameObject videoPlayer02;
        public GameObject videoPlayer03;
   
        void Start()
        {
            videoPlayer01.SetActive(false);
            videoPlayer02.SetActive(false);
            videoPlayer03.SetActive(false);
        }
   
   
       void OnTriggerEnter(Collider player)
       {
           if (player.gameObject.tag == "Player01")
           {         
               int prefabIndex = UnityEngine.Random.Range(0, prefabList.Count - 1);
               videoPlayer01.SetActive(true);
               videoPlayer02.SetActive(true);
               videoPlayer03.SetActive(true);
   
           }     
       }
   }

Put your video game objects in a forloop, loop through that list, and play the one that matches the randomly generated number.

//in your variables
public GameObject[] videos;

//in your trigger enter
int selection = Random.Range(0, videos.length);
for(int i = 0; i < videos.length; ++i){
    videos.SetActive(i == selection);
}

Personally I’d go further to grab the video out of streaming assets via file path, and spawn the prefab to play it, but that depends on your knowledge of the video player and streaming assets.

3 Likes

I thought that looked a little strange, there are a couple errors. I like what you did in the for loop–set everything to false except the chosen one, with just one line, nice.

public GameObject[] videos;
int selection = Random.Range(0, videos.Length);
        for (int i = 0; i < videos.Length; ++i)
        {
            videos[i].SetActive(i == selection);
        }
1 Like

Thank you!