E
E
Evgeny Ivanov2021-07-09 14:05:21
Unity
Evgeny Ivanov, 2021-07-09 14:05:21

How to connect one object to several scenes?

I'm doing a simple example - switching between scenes. Everything is working.
60e8285d1ccdf504043699.png

As you can see in the picture, a SceneChanger object has been created on the scene.
A script with the same name SceneChanger is connected to this object.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneChanger : MonoBehaviour
{
public void ChangeScene(int sceneNumber)
{
SceneManager.LoadScene(sceneNumber);
}
}

The OnClick button event is called by this script with the scene number.

A couple of questions arose:
1) How to connect one object (SceneChanger ) to several scenes?
The SceneChanger object is now only on the first scene. And I need to connect it to the button on the second stage and to the button on the third stage.
Of course, I can copy it (create an object on each scene), but this will not be entirely true.
I will have three identical "empty" objects. And it seems that one script will be connected to them, but it’s still somehow strange to duplicate the same entities.

2) If I connect a script to a visual component, do I always have to do it through another object?
That is, I connected the SceneChanger.cs script to an empty SceneChanger object. As if directly.

But I cannot connect the SceneChanger.cs script to an Image (or Button) object simply by dragging it.
Then I will not see the class and method I need in the menu list. Therefore, I have to connect the script "through an intermediary" - connect the SceneChanger object to the Image, to which the SceneChanger.cs script is already connected.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-07-09
@freeExec

1) Save a set of buttons and other necessary in prefab and use it in the future on all scenes. By changing this prefab, it will be changed wherever it is used.
2) There are no restrictions on hanging a script on a picture or a button if it is MonoBehaviour.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question