Saving UI TMP Button's Onclick Action in Inspector

When I am in the game and change the scene, the click field isn’t carried to the new scene.


This is an example of one of the buttons. When I change to a different scene in game, this field is empty. I wanted to know if there was a way to assign that field in script when it’s loaded.

Edit: It seems the image isn’t showing here. What I’m trying to show is the OnClick box inside the button component and the feilds inside when a new index is added.

Don’t change where the button maps.

Think of the button as a simple intention. It means:

“The user wants to go to the next scene”

Now, map that to some logic in your game that does know what the next scene should be, then does the correct thing.

Such logic is often kept in whatever you are using as a GameManager that lives for the duration of the game.

If you don’t have a GameManager, make one, and there’s tons of tutorials covering the basic process.

I’m not having a problem with actually changing the scene. It’s all my buttons that don’t contain an input after I load a new scene. The button is also part of a Prefab.

I don’t know what this means.

Did the button persist from scene to scene but the thing servicing it did not?

Debug that by pausing in each scene and studying what is in your hierarchy.

The button itself persists and all the indexes in the OnClick array persist, it’s just that the field in each of index’s boxes are empty. It shows the words “Missing (Object)” in the field where I would add the game object that contains the script.

This means that the GameObject that this script instance is connected to no longer exists.

Before changing scenes, click on that field so you can tell which object it is in the scene, then rename that object to something else, change scenes and you’ll see it is gone.

Fix that. :slight_smile:

I’ve been trying to do what you said, I press play, change the name of the GameObject (which is a prefab), then press the button that changes to a new scene. But the name is still the same in the new scene. I use some code to identify whether the instance of the script is the same as what it should be.

private ScriptName instance;
private void Awake(){
if(instance == null{
   instance = this;
   DontDestroyOnLoad(instance);
}
else if (instance != this){
Destroy(gameObject);
}
}

Fixed the issue. It was the code in the latest reply that showed the instance destroying it’s holder. I removed the Destroy(gameObject) part.

1 Like