Im trying to make a Vuforia Video Player with virtual buttons but when I try to pause and play it gives me and error. I looked at some forums and some question that is old but they didnt fix my problem. Error is:

Assets\vp_time.cs(23,9): error CS0120: An object reference is required for the non-static field, method, or property 'VideoPlayer.Pause()'Assets\vp_time.cs(27,9): error CS0120: An object reference is required for the non-static field, method, or property 'VideoPlayer.Play()'

Code is:

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.Video;using Vuforia;public class vp_time : MonoBehaviour{public GameObject vbBtnObj;public GameObject vbVpObj;// Start is called before the first frame updatevoid Start(){vbBtnObj = GameObject.Find("VideoBtn");vbBtnObj.GetComponent<VirtualButtonBehaviour>().RegisterOnButtonPressed(OnButtonPressed);vbBtnObj.GetComponent<VirtualButtonBehaviour>().RegisterOnButtonReleased(OnButtonReleased);vbVpObj.GetComponent<VideoPlayer>();}public void OnButtonPressed(VirtualButtonBehaviour vb){VideoPlayer.Pause();}public void OnButtonReleased(VirtualButtonBehaviour vb){VideoPlayer.Play();}// Update is called once per framevoid Update(){}}
1

Best Answer


Well as the error says VideoPlayer is a type. It has no static method VideoPlayer.Play. You need a reference to an instance of type VideoPlayer and call the method on that instance.

vbVpObj.GetComponent<VideoPlayer>();

this line does absolutely nothing!

You rather wanted to store this reference in a field

// Drag this already in via the Inspector in Unity[SerializeField] private VideoPlayer videoPlayer;

or do

private void Start (){...// As fallback get the reference on runtime ONCEif(!videoPlayer) videoPlayer = vbVpObj.GetComponent<VideoPlayer>();}

and later

videoPlayer.Play();

and

videoPlayer.Pause();