Hello, for a few hours now I have been trying to resolve an issue I am having trying to reference a method in another script and I keep getting the compiler error "An object reference for the non-static field, method, or property ‘x’ "
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
This is the program where I am referencing "GameOverScreen"
public class LavaMovement : MonoBehaviour
{
public float speed = 2f;
void Start()
{
Time.timeScale = 1;
}
void Update()
{
transform.Translate(Vector3.up * Time.deltaTime * speed);
}
private void OnTriggerEnter(Collider trigger)
{
if(trigger.gameObject.tag == "Player")
{
Time.timeScale = 0;
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
GameOverScreen.Setup();
}
}
And this is GameOverScreen:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameOverScreen : MonoBehaviour
{
void Start()
{
}
public void Setup()
{
gameObject.SetActive(true);
}
public void RestartButton()
{
SceneManager.LoadScene("SampleScene");
}
public void ExitButton()
{
Application.Quit();
}
}
I know it is probably a simple fix and I am being dumb but this has been happening with alot of my code and I usually just turn variables to static. No other solutions online really worked for me so I’m just confused on what I am supposed to do. Thankyou for reading
Plus 1 for trying for hours it is the only tried and true way forward. You are referencing a class “Name” not an object created of that type. A class declaration is a “template” used to generate an object. So LavaMovement is not referencing an object when it tries to call Setup(). The error is telling you that it “could” if you declared the Setup method as static. You don’t want to do that but the editor is giving you options it guesses at not knowing what you really meant.
Thanks. I feel like I wrote the right code at least 5 times but I guess the solutions had me adding other stuff that maybe why I scuffed the code, I am so happy I found help here. Your explanation also really helps alot, now I know I shouldn’t always trust the compiler error messages fully. I don;t really understand the class and names bits so I will try looking into that thanks.