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 update
void 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 frame
void Update()
{
}
}
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 ONCE
if(!videoPlayer) videoPlayer = vbVpObj.GetComponent<VideoPlayer>();
}
and later
videoPlayer.Play();
and
videoPlayer.Pause();
Related
I've been using Unity to create a simple 2D game but the problem is that even when the game object "player" gets destroyed, the gameobject "isDead" (text) doesn't appear.
This is my script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class youDied_Text : MonoBehaviour
{
private Transform player;
private Text isDead;
// public static bool isDead;
// Start is called before the first frame update
private void Start() {
isDead = GetComponent<Text>();
}
void checkForDeath()
{
if (player==false)
{
isDead.gameObject.SetActive(true);
}
else
{
isDead.gameObject.SetActive(false);
}
}
// Update is called once per frame
void Update()
{
player = GameObject.FindWithTag("Player").transform;
checkForDeath();
}
}
This script is attached in the text which I need to display in UI element.
As was noted currently you would get a NullReferenceException which is definitely not what you want.
There is absolutely no need / redundancy going through Transform at all actually. Simply store the GameObject reference instead
You are currently setting the object to inactive which has the Text attached ... which is the same object your component is attached to as well!
=> As soon as you end up in the second case once you set it to inactive => from now on Update is never called anymore!
In general as it sounds like this should only happen once anyway I would use a more event driven approach and have a component on your player like e.g.
public class Player : MonoBehaviour
{
public UnityEvent onDied;
private void OnDestroy ()
{
onDied.Invoke();
}
}
And then simply attach a listener/callback to that event once without poll checking states. You can do this either via the Inspector directly (just like in e.g. Button.onClick) or via code like e.g.
public class youDied_Text : MonoBehaviour
{
// Already reference things via the Inspector if possible!
[SerializeField] private GameObject player;
[SerializeField] private Text isDead;
private void Awake()
{
if(!isDead) isDead = GetComponent<Text>();
isDead.gameObject.SetActive(false);
// If you want to rather set it via the Inspector remove all the rest here
//if(!player) player = GameObject.FindWithTag("Player"). GetComponent<Player>();
// or even simpler
if(!player) player = FindObjectOfType<Player>();
player.onDied.AddListener(OnPlayerDied);
}
// If you want to rather set it via the Inspector make this public
private void OnPlayerDied()
{
isDead.gameObject.SetActive(true);
}
}
I have a problem that when i change scenes from the menu to the game and when i click i will get the error 'The object of type 'GameObject' has been destroyed but you are still trying to access it.' and this is happening while the menucontroller is not in the game scene, i have been working on this for the past few days to fix it but cant find how to do it. If anyone can help that would be great this kind of question has been asked alot but I cant find a solution for my problem.
We also have a similar script that has the same code except instead of sceneswitching its for interaction so when we try to interact with something we get the error
Thank you for your help.
menucontroller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using API;
public class MenuController : MonoBehaviour
{
[SerializeField] private InputActionReference shoot;
[SerializeField] private InputActionReference shoot2;
public GameObject sendObject;
public GameObject sendObject2;
public float range = 1000;
void Start()
{
shoot.action.performed += Shoot;
}
async void Shoot(InputAction.CallbackContext obj)
{
RaycastHit hit;
if (Physics.Raycast(sendObject.transform.position, sendObject.transform.forward, out hit, range))
{
SceneManager.LoadScene("SampleScene");
}
}
}
The error message sounds quite self-explanatory.
You are trying to access an object that already was destroyed.
Since you load a new scene
SceneManager.LoadScene("SampleScene");
the current scene and its objects are unloaded / destroyed.
Whenever you deal with events
void Start()
{
shoot.action.performed += Shoot;
}
make sure to also remove the callback as soon as not needed/valid anymore
private void OnDestroy()
{
shoot.action.performed -= Shoot;
}
This question already has answers here:
CS0120: An object reference is required for the nonstatic field, method, or property 'foo'
(9 answers)
Closed 2 years ago.
I keep getting this error, but I don't even know what is or isn't static in this context? I have tried solutions like setting instances and checking capitalization but I just get the same error. I want the shop script to change the monney value, which is written into debug until I set up the right U.I.
The Zoney script:
using UnityEngine;
using UnityEngine.UI;
public class Zoney : MonoBehaviour
{
public Text Money;
public int Monney;
private string Mony;
// Start is called before the first frame update
void Start()
{
Money = GetComponent<Text>();
}
public void setMonney(int Change)
{
Monney = Change;
}
// Update is called once per frame
void Update()
{
Mony = Monney.ToString();
Money.text = Mony;
}
}
The Shop script:
using UnityEngine;
public class Shop : MonoBehaviour
{
public int Change;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Change += 1;
Zoney.setMonney(Change);
Debug.Log(Change);
}
}
Because you're working with Unity, you need to follow the requirements of the engine.
In a case like this, where you need an instance of a component (MonoBehaviour), you really want to be referencing a Unity created instance, instead of creating a new one with the new keyword. Creating an instance of a component using the new keyword is going to leave you with an instance of the class that's not associated with any Unity GameObject.
The far more reliant way to get the component you want to reference, is to use the Inspector window, and drag into the object field, the right component on the desired object. In this case, I am going to assume you'll want to drag a Scene object (in your hierarchy) into the object field slot.
You would do that by first defining a variable. This can generally be done in one of two ways:
public Zoney zoney;
[SerializeField] private Zoney zoney;
In this example, once you've assigned your reference, use the variable zoney, instead of the class name Zoney. Note that your variable name could be anything else you feel is appropriate, e.g. _zoney, or myZoney.
Your new Shop script could then look like this:
public class Shop : MonoBehaviour
{
public int Change;
public Zoney zoney;
void Update()
{
Change += 1;
zoney.setMonney(Change);
Debug.Log(Change);
}
}
Zoney is a class, you need to create an instance of it first before using it.
Instantiate a class that derives from MonoBehaviour
Also very important, you need to update your shop object so it has the Zoney instance as a member object otherwise your updates to money wont be kept:
i.e.
public class Shop : MonoBehaviour
{
private Zoney;
public int Change;
// Start is called before the first frame update
void Start()
{
_HiddenZoney = gameObject.Addcomponent<Zoney>();
}
// Update is called once per frame
void Update()
{
Change += 1;
_HiddenZoney.setMoney(Change);
Debug.Log(Change);
}
}
Thanks #derHugo for the alert!
You need to create a object from Zoney class to access it's non static memebers. Try below:
public class Shop : MonoBehaviour
{
public int Change;
public Zoney myZoney; // Need to create the object
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Change += 1;
myZoney.setMonney(Change); // Access members using created object
Debug.Log(Change);
}
}
I have the problem that I can't access my own script, that I attached to my image object, from another script.
I can access the Image(Script) that comes when I create the image object but that's all.
Error: object reference not set to an instance of an object.
My GameManager script is the one trying to access my custom image script and this is how:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class scriptGameManager : MonoBehaviour {
public Image img1;
private int gmValue = 0;
void Update () {
gmValue = img1.GetComponent<MyImageScript>().GetValue();
}
}
and the Script I attached to my Image Object is:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MyImageScript: MonoBehaviour {
public int GetValue () {
return 10;
}
}
Any ideas what I'm doing wrong?
I can access the Image(Script) that comes when i create the image
object but that's all
If I am correct, you are trying to access MyImageScript from the scriptGameManager script and the MyImageScript is attached to the-same GameObject img1(Image) is attached to.But according to the image in your comment, you do NOT have MyImageScript script attached to the Image.
The only script that is attached to your Image is the ScriptCharSpot script.
You must attach MyImageScript to your image within the Editor or the through code before you can use GetComponent on it or else, it will return null. At the-same time, you need to cache the MyImageScript script instead of calling GetComponent each frame.
public class scriptGameManager : MonoBehaviour {
public Image img1;
MyImageScript imgScript;
private int gmValue = 0;
void Start () {
//Add MyImageScript to img1
img1.gameObject.AddComponent<MyImageScript>();
//Get/Cache MyImageScript that is attached to img1
imgScript = img1.GetComponent<MyImageScript>();
}
void Update () {
gmValue = imgScript.GetValue();
}
}
I want this class to render a grid each time it is instantiated, but I am getting the error, error
CS0120: An object reference is required to access non-static member
`UnityEngine.GameObject.GetComponent(System.Type)'
So I instantiate an object of Renderer called Rend and set that equal to the non-static member but I am still getting the error? Any help would be greatly appreciated as I have researched for several hours and still can't figure this out.
using UnityEngine;
using System.Collections;
public class SetGrid : MonoBehaviour {
public int x = 1;
public int y = 1;
void Start()
{
Renderer Rend = GameObject.GetComponent<Renderer>().material.mainTextureScale = new Vector2 (x, y);
}
}
I am assuming this script is part of your grid game object which also has a component of type Renderer.
The correct syntax to scale the texture of the Renderer is following:
public class SetGrid : MonoBehaviour {
public int x = 1;
public int y = 1;
void Start()
{
// Get a reference to the Renderer component of the game object.
Renderer rend = GetComponent<Renderer>();
// Scale the texture.
rend.material.mainTextureScale = new Vector2 (x, y);
}
}
The error message was thrown because you tried to call GetComponent on a type of GameObject and not an instance of GameObject. The script itself is already in the context of a GameObject meaning that you can just access a component with GetComponent from a non-static method.
The problem is not with the Rend object, it's caused by the fact that the GetComponent method is non-static. That means you need to instantiate a GameObject object and use that one to call GetComponent.
Change GameObject.GetComponent to gameObject.GetComponent, so that you're referencing the gameObject that this MonoBehaviour script belongs to.
Or you can even not use gameObject.GetComponent and just use GetComponent by itself.
See the MonoBehaviour inherited members for details.