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

Re trigger A Video

Discussion in 'Scripting' started by InFocusAV, May 15, 2020.

  1. InFocusAV

    InFocusAV

    Joined:
    May 5, 2020
    Posts:
    2
    Hello,

    I am quite new to unity and C# and have been learning through some online tutorials which is great!

    I have come unstuck at a point of the game where I want to trigger a video with my player,
    Using the example I have found I can destroy the component after a number of seconds, and this works by starting the video on my textured screen, then destroys the component after int timeTo Stop.

    However what I ultimately want to achieve is for the video to be triggered from the start each time the player collides with the trigger.
    Is there some pointers as to what I should be looking for to achieve this?


    The code I have so far is;

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayVideo : MonoBehaviour
    6. {
    7.     public GameObject videoPlayer;
    8.     public int timeToStop;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         videoPlayer.SetActive(false);
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void OnTriggerEnter (Collider player)
    18.     {
    19.         if (player.gameObject.tag == "Player")
    20.         {
    21.             videoPlayer.SetActive(true);
    22.             Destroy(videoPlayer, timeToStop);
    23.         }
    24.  
    25.     }
    26.  
    27. }
     
  2. InFocusAV

    InFocusAV

    Joined:
    May 5, 2020
    Posts:
    2
    Managed to make it work..
    added the following to the code, unless someone knows of a cleaner or a different way to do this ?

    Code (CSharp):
    1.  
    2.     void OnTriggerExit(Collider player)
    3.     {
    4.         if (player.gameObject.tag == "Player")
    5.         {
    6.             videoPlayer.SetActive(false);
    7.         }
    8.     }
    9.  
    10. }