Using UI button to change movie textures on object

Hi there,

I’m just new to Unity and for a project I’m trying to change movie textures on an object (cube) with an UI button.
I’m able to change the textures with the space bar (see example script below) but I can’t figure out how to use an UI button instead of the space bar.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class CreateTexture : MonoBehaviour {
    public Texture[] textures;
    public int currentTexture;

    void Start()
    {

    }

    // Update is called once per frame
    void Update() {

        if (Input.GetKeyDown(KeyCode.Space))

        {
            currentTexture++;
            currentTexture %= textures.Length;
            GetComponent<Renderer>().material.mainTexture = textures[currentTexture];
        }

    }
}

So above script does exactly what I want. I just want to use an UI button instead of the spacebar :slight_smile:

https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button

unity tutorial :slight_smile:

Cool this already helped out! Now I’m able to change the materials through a UI button click :slight_smile: Thing is, the movie (set as material) won’t play anymore. I do have a ‘play movie script’ on the object though:

using UnityEngine;
using System.Collections;

public class PlayMovie : MonoBehaviour {

    // Use this for initialization
    void Start () {

        MovieTexture movie = GetComponent<Renderer>().material.mainTexture as MovieTexture;
        movie.Play();
        movie.loop = true; 
    }
   
    // Update is called once per frame
    void Update () {
   
    }
}

If I just assign a material - with a movie assigned to it - to the object it works perfectly well. However when the material is changed with the UI button, the script doesn’t start the movies anymore.

Any thoughts on this?

Nevermind, I think I got this! Of course I need to put my code in the void update, otherwise it won’t load when the material is changed :slight_smile: