S. Acarsoy
S. Acarsoy

Reputation: 35

How to Activate RawImage on button click in Unity?

I just started learning Unity and trying to create a Video Gallery, for now I created a button with the Red Cross shown on the image. And the bigger rectangle is a raw image that holds my video. Buttons and videos I would like to add a thumbnail on the button and on click the button the RawImage will start the corresponding video. For now it is working as mentioned but if only I activate the RawImage. I don't want the RawImage to be seen before I click the button.

For now this is what I have;

    RawImage rawImage1;
    public void StartVideoOne()
    {
        rawImage1.SetActive(true);
        videoPlayer.Play();

    }

But if I try to activate the RawImage during button onClick call this does not start the video. How can I solve this problem?

Upvotes: 0

Views: 2032

Answers (1)

BFyre
BFyre

Reputation: 395

First of all, you cannot use SetActive on RawImage reference, it has to be a GameObject. I assume though you only put it in the code snippet so it's clear to see what's going on.

I set up a VideoPlayer in simple test project and your code worked just fine to activate the object and play the video, so here are a few things you might have missed:

  • Have you checked that your button works? Do you have GraphicRaycaster component in your canvas? Did you subscribe to button's onClick event, either via inspector or code? Do you have EventSystem on scene?
  • If your render mode in VideoPlayer is Render Texture, did you assign your RawImage as Target Texture?

This is an example of what you need to have: enter image description here

And code:

public class PlaybackController : MonoBehaviour
{
    public Button playButton;
    public RawImage rawImage1;
    public VideoPlayer videoPlayer;

    private void OnEnable()
    {
        playButton.onClick.AddListener(StartVideoOne);
    }

    private void OnDisable()
    {
        playButton.onClick.RemoveListener(StartVideoOne);
    }

    private void StartVideoOne()
    {
        rawImage1.gameObject.SetActive(true);
        videoPlayer.Play();
    }
}

Upvotes: 1

Related Questions