Null reference, activating function from another script, as it is not shown on the button onclick

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MainMenuLoad : MonoBehaviour
{
public Button button;
public void Load()
{
GetComponent().LoadPlayer();
}
}

this is the script that should do the function from the player script that is called LoadPlayer

the part from the player script is:

public void SavePlayer ()
{
SaveSystem.SavePlayer(this);
}
public void LoadPlayer ()
{
Vector3 position;
PlayerData data = SaveSystem.LoadPlayer();
position.x = data.position[0];
position.y = data.position[1];
position.z = data.position[2];
transform.position = position;
}
}

NullReferenceException: Object reference not set to an instance of an object

The answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

This is the kind of mindset and thinking process you need to bring to this problem:

Step by step, break it down, find the problem.

Here is a clean analogy of the actual underlying problem of a null reference exception:

Null reference NEVER requires a forum post, but in the future, if you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

I will try what you have said, but I still don’t know why its not showing up the functions to trigger when the button is pressed, on the list of actions on press. The game object is on another scene so Im guessing that might be it, isn’t there an easier way to set this up without coding it?

Is this why?

https://discussions.unity.com/t/741309/6

I already knew I had to reference the game object the script was on, but how do I drag the game object if its in another scene, and the button in another one? If I switch scenes I lose the inspector of the button on the right
Edit: I moved my scene with the player into the one with the button so that both windows would be open at the same time, now I see that even if I drag the player directly onto the onclick it doesn’t accept it, as if I’m not dragging anything in

I tried selecting the gameobject directly, but its not showing up on the list with all the assets I have

If you’re doing multi-scene stuff, you pretty much need to centralize all your input in a single scene, and have that only connected to your GameManager, also in that scene. Then other objects can contact the GameManager to subscribe to interesting events.

I actually wrote a package I call Datasacks to marshal all Unity UI input down to code, where pretty much anything anywhere in your game can listen for events they want, finding them by name, but also with the benefit of code generation for compiler safe naming.

8687265--1171653--20180724_datasacks_overview.png

Datasacks is presently hosted at these locations:

https://bitbucket.org/kurtdekker/datasacks

Finally works, changed how my saving worked a bit, used a bunch of player prefs connected to one another until it worked