Referencing a Method in another Script - Beginner

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 :slight_smile:

Referencing GameObjects, Scripts, variables, fields, methods (anything non-static) in other script instances or GameObjects:

2 Likes

Thanks that helps alot, however I got another error. “Object reference not set to an instance of an object”, how do I fix this?

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.

1 Like

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.

First pinned post in the forum. The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that
1 Like