- Home /
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 !!
Answer by dmg0600 · Sep 21, 2014 at 06:41 PM
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
Typically you would run the database stuff on a different thread. Any long running task that doesn't touch UnityEngine should be considered for multi threading.
Answer by Slev · Sep 21, 2014 at 06:42 PM
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 ?
Your answer
Follow this Question
Related Questions
C# Unity 3d Don't Destroy Class On Load 1 Answer
C# Accessing Variables from Class Variable 1 Answer
C# Number Key For Loop 1 Answer
C# Character Controller Collision If Statement 2 Answers
unity c# RPC 0 Answers