How to execute a Skript global?

The Problem is the following: I have several Databse Connection Scripts and i have to include them in every Level i make. And now the script is always executing and resets the variables which takes much time and performance. What can i do to access it Once and through different levels without resetting the parameters and classes?
I want to call it once at the beginning and change the variables from now on by myself in every level i have !!

2 Answers

2

You can put that component into one GameObject alone that will not be destroyed when changing scenes. To let Unity know that you don’t want it to destroy that GameObject you have to put the next code in your script:

void Start()
{
    DontDestroyOnLoad(gameObject);
}

This way the GameObject won’t be destroyed when changing levels and its variables will remain the same.

is there any other way to do it? If not how do i attach the script to another gameobject? you know i have more than 1 gameobject in my scene and to implement a passage to another script you have to drag the gameobject with the script to the public variable how do i do this ?

For organizing purposes it would be better make all your connections through only one script and use DoneDestroyOnLoad in that script. This way it is only one GameObject which has to be kept alive through the execution of your game and all the other GameObjects that need to comunicate with the database would do it using the one with the connections. Doing it like this you won't have to keep alive all the GameObjects that connect to the database but just one.

basically i have just one base class with all the connections. but that was not the question my question was how i can assign the script of a gameobject to a variable when the gameobject doesn't exist? i mean when i edit unity does not show objects which are not destroyd in another level does it? so what do i do to assign it?

You will have to find it first. As it is sort of a connection manager I suggest you make it a singleton. public static YourClass Instance; void Awake() { if (GameObject.FindObjectOfType(typeof(YourClass)) == null) { Instance = this; } } This way you can access the class from wherever through YourClass.Instance

It doesn't work it always says NullReferenceException let's see if i get this correctly i have to call this code in the script i'm trying to access right? i have in my code: public static Cards CardNS; void Awake() { if(GameObject.FindObjectOfType() == null) CardNS = this; } in another code i said (for testing) Debug.Log(Cards.CardNS.AllCards[0].name); in an update routine and it continued giving me this exception so what did i made wrong?

You just need to attach all the scripts to an Object and then call DontDestroyOnLoad this will ensure the object is carried with you.

is there any other way to do it? If not how do i attach the script to another gameobject? you know i have more than 1 gameobject in my scene and to implement a passage to another script you have to drag the gameobject with the script to the public variable how do i do this ?