Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kokujou · Sep 21, 2014 at 06:35 PM · c#classlevel

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 !!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 25 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kokujou · Sep 21, 2014 at 09:17 PM 0
Share

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 ?

avatar image dmg0600 · Sep 21, 2014 at 09:52 PM 0
Share

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.

avatar image Kokujou · Sep 21, 2014 at 10:29 PM 0
Share

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?

avatar image dmg0600 · Sep 21, 2014 at 10:36 PM 0
Share

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

avatar image Kiwasi · Sep 24, 2014 at 10:17 PM 2
Share

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.

Show more comments
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kokujou · Sep 21, 2014 at 09:18 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges