- Home /
Assign video in inspector via script
Hi,
I've been trying to figure out how to assign a variable in inspector via script, from what I've found it seems you have to first search for the game object and then use GetComponent. However, all the examples I have seen are editing images and using spriterenderer mostly, not actually assigning one thats attached to a script.
I have a canvas that has a child which is a RawImage (just called RawImage as well), with a script attached to the RawImage that plays a video. The video plays fine, however if I assign it via the inspector that is the only video that is able to be played, I want the video to be played based on a variable that contains a video file name (meaning it has to be done via script and can't be manually assigned as it will constantly change).
My script is:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
[RequireComponent (typeof(AudioSource))]
public class PlayVideo : MonoBehaviour {
public MovieTexture video;
private AudioSource audio;
public string videoName;
// Use this for initialization
void Start () {
// videoName = Character_Mode.name // this gets the string from another script that contains the video file name which is what I want to assign
GetComponent<RawImage> ().texture = video as MovieTexture;
audio = GetComponent<AudioSource> ();
audio.clip = video.audioClip;
video.Play ();
audio.Play ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Space) && video.isPlaying) {
video.Pause ();
} else if (Input.GetKeyDown (KeyCode.Space) && !video.isPlaying)
video.Play ();
}
}
For clarification here's how everything looks
tldr; I want to use a video file name stored in a string, and assign that to the video in inspector, but via script.
Thanks in advance, I'm new to Unity and C# so sorry if it as simple as finding the object and using GetComponent, I just really can't work it out and can't find any similar examples! :(
Your answer
Follow this Question
Related Questions
Can I add an enum value in the inspector? 4 Answers
Arrays/Lists in the inspector of Editor Extension scripts 0 Answers
There is no GameObject attached to this GameObject 2 Answers
C# script as variable 1 Answer
Reorderable list of UnityEvents 2 Answers