F
F
Farset2018-07-24 17:40:59
C++ / C#
Farset, 2018-07-24 17:40:59

How to use one script for multiple related scenes?

There are several scenes in my game (calculators, they will need to be upgraded later), they differ only in design and I need to make sure that each calculator has its own points and item levels and I absolutely don’t know how to implement it.
Note: to return to the main menu without leaving the game, you need to enable the visibility of the "More" panel in the Canvas section, there will be a corresponding button!
Project link: https://yadi.sk/d/q3IK51Za3ZXMBL

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GreatRash, 2018-07-24
@Farset

1) You need to create an empty GameObject
2) Your script-calculator (or whatever you have) already clings to this object
3) In the script, set the DontDestroyOnLoad flag to the object
4) If the flag is true, then the object will not be unloaded when changing scenes (read the object will always be present in the game)

using UnityEngine;
using UnityEngine.SceneManagement;

public class MyEternalObject: MonoBehaviour {
  private static bool created = false;

  void Awake() {
    if (!created) {
      DontDestroyOnLoad(this.gameObject);
      created = true;
    }
  }

  // остальной код
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question