problem playing movie in C#

I can play my video on a texture with this javascript code like in the manual but my whole project is in c# and i cant figure out how to write tis in c#.
stared myself to death
does anyone has a solution?
thx

function Update () {
	if (Input.GetButtonDown ("Jump")) {
		if (renderer.material.mainTexture.isPlaying) {
			renderer.material.mainTexture.Pause();
		} else {
			renderer.material.mainTexture.Play();
		}
	}
}

Could you provide your C# script that does not work?
Or are you asking for what you need to change to use that in c#?

the later is just: replace function with void, put it into a class

public class test : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		renderer.material.mainTexture.Play();
	}
}

I tryed this before. I attashed it to my videotexture.
this gives me always an error :
Assets/Scripts/Movie/test.cs(13,47): error CS0117: UnityEngine.Texture' does not contain a definition for Play’

so just made this in js.
fixed it by placing the js function in standard assets and call it in my c#.
But i don’t like this way of working.

ainTexture is not needfully a movie texture, you need to cast.

JS simplifies this kind of stuff as it does that all behind the scenes but this also makes it significantly harder to solve problems where you are forced to do it yourself as you never needed to think about it.

exactly. So, in C# you’d want to do something like

VideoTexture vt = renderer.material.mainTexture as VideoTexture;
vt.Play();

or, shorter:

((VideoTexture)renderer.material.mainTexture).Play();

JS is (by default) not strict about the typing, it doesn’t need to know the type of a variable, and will find the member fields and methods through reflection - but this comes at a significant performance cost.

Thanks. It’s more clear now.

I think you mean MovieTexture instead of VideoTexture. Probably just a typo.

1 Like