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
1
Question by bustedkrutch · Jul 23, 2013 at 07:56 PM · dontdestroyonloadstatic-variablesstatic-function

Trouble with DontDestroyOnLoad

Hi Gurus,

I'm having a heck of a time with what should be a simple operation.

I'm using DontDestroyOnLoad(this.gameObject) on an empty object with scripts that I'm using as a game controller for variables, scores, etc. for the scope of the game.

The object, gameController, is loaded in the first scene and then I load the second scene and I'm attempting to access variables and scripts in gameController

I've put the following in the AWAKE and START functions in the script called "GameController" which is a component of the empty object called "gameController" [upper case G on the script, lower case g on the object - no particular reason] (tried AWAKE and START individually and together)

 DontDestroyOnLoad(transform.gameObject);
 DontDestroyOnLoad(this.gameObject);
 DontDestroyOnLoad(this);

I think this is overkill but still not working.

Here is (hopefully the relevant portions of) the script in the second scene that I'm attempting to access it with:

var GC : GameObject; // gameController Object (lower case g)

var GCS : GameController; // GameController Script (upper case G)

[inside START function]

 // Find GameController script and assign a local reference
 if (GameObject.Find("gameController") == null){
 Debug.Log("get returns null");
 }
 GC = GameObject.Find("gameController");
 GCS = GC.GetComponent("GameController");


The null warning is issued in the Console and no access.

I've googled the life out of this and read many forum answers, and they all seem to point in the same direction that I believe that I'm already going.

Help in this is very much appreciated, as development is at a screeching halt...sigh

Thanks up front!

Comment
Add comment · Show 4
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 Jamora · Jul 23, 2013 at 08:13 PM 0
Share

By the looks of your snippets, I'd venture a guess and say the GameObject that has the GameController script isn't named "gameController". Every one of those DontDestroyOnLoad you try should prevent the destruction of the gameobject... Is the controller object still in the scene hierarchy after the second scene loads?

avatar image bustedkrutch · Jul 23, 2013 at 08:42 PM 0
Share

Thanks for your response Jamoro, but yeah, the gameController object name is "gameController" (lower case g), and the script is called "GameController" (upper case G).

Real creative, I know.

Other ideas (please!)?

avatar image Jamora · Jul 23, 2013 at 09:08 PM 0
Share

You wrote the Start() and Awake() functions in CAPS, did you write them in caps in your code as well? Only the first letter should be capitalized...

avatar image bustedkrutch · Jul 23, 2013 at 09:25 PM 0
Share

Thanks Jamora, but yeah, I don't know why I typed them in caps for this question, but they are correct in the script "Awake" and "Start".

Great detail catching, and I'll try to be more accurate, cause after all you guys are helping me look for the detail I'm missing. Thanks.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by ErikBoyeAbrah · Jul 23, 2013 at 08:28 PM

(Edit)

I made a little demo to change Scenes as a test.

  1. I took my EmptyGameObject(Manager) and made it a prefab by dragging it into a prefab folder I made in my assets.

  2. I then made a new script C# called OnLoad.

    using UnityEngine; using System.Collections;

    public class OnLoad : MonoBehaviour {

      void Awake(){
             
             DontDestroyOnLoad(transform.gameObject);
             
         }
     }
    
    
  3. Then I put the OnLoad script on my Manager prefab

  4. When I then changed the scene my manager was still in the next scene, so this was a success.

I hope this helps.

(Edit)

I accidentally removed the link to the game state manager, her is it again. http://www.fizixstudios.com/labs/do/view/id/unity-game-state-manager

Comment
Add comment · Show 10 · 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 bustedkrutch · Jul 23, 2013 at 09:02 PM 0
Share

Thanks for your respone ErikBoyeAbrah,

I attempted to do as you suggested.

I went into the script and added this

public var game_controller : Transform;

Then went to the inspector and dropped the object "gameController" into it, however the second scene is still unable to "find" it.

I also added the "Static" check mark in the inspector of "gameController" in case I was missing something, and still nope.

Thank you very much, if you think of something else I would appreciate hearing about it.

avatar image ErikBoyeAbrah · Jul 23, 2013 at 09:17 PM 0
Share

When you go to your new scene is your gameobject in the Hierarchy?

avatar image bustedkrutch · Jul 23, 2013 at 09:27 PM 0
Share

Thanks ErikBoyeAbrah,

No, it isn't. I'm thinking that if it was it would be a different instance of the object and whatever variables were in it.

Should it be? Do I need to include it in all of the scenes I want it to have access to it?

avatar image ErikBoyeAbrah · Jul 23, 2013 at 09:29 PM 0
Share

Read the edit in my first post it says something important about GameObject.Find

But again about the game state manager is actually for more complex games

avatar image bustedkrutch · Jul 23, 2013 at 09:36 PM 0
Share

Was that all there before, man, I'm blind.

Show more comments
avatar image
2

Answer by Bunny83 · Jul 23, 2013 at 09:42 PM

Make sure that your "gameController" gameobject is actually a root GameObject. If it's a child of another GameObject DontDestroyOnLoad won't do anything since if the parent is destroyed the childs will also be destroyed.

Change your "searching" to this:

 GCS = FindObjectOfType(GameController);


You might also add a static variable to your GameController class which holds a reference to the one instance in the scene:

 // in your GameController.js
 static var instance : GameController;
 
 function Awake()
 {
     transform.parent = null; // force top-level GameObject just in case
     instance = this;
     DontDestroyOnLoad(gameObject);
 }

Now you can access your GameController from any other script like this:

 GameController.instance
Comment
Add comment · Show 4 · 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 bustedkrutch · Jul 24, 2013 at 01:01 AM 0
Share

Hi Bunny83,

I was able to get something working from an earlier post, but I'll try your suggestion also, as I really would like to have a thorough understanding of how to do this.

I'll respond after I try it this evening.

Thank you for responding.

avatar image bustedkrutch · Jul 24, 2013 at 02:39 AM 0
Share

Hi Bunny83,

Thank you for your response. I just tried your solution and it worked just as you thought it would.

Thank you again.

avatar image vvander · Apr 29, 2014 at 07:40 PM 0
Share

The first two sentences are gold, thank you. It seems intuitive, but I forgot about this and I suspect others could as well.

avatar image Kowciany · Mar 17, 2015 at 05:03 PM 0
Share

Yep ! Great answer ! I was wondering why DontDestroyOnLoad doesn't work. Now it works like a charm !

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

21 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

Related Questions

reloading game from main menu problem 1 Answer

donotdestroy for hud - but hud appears on main menu :( 1 Answer

how to Undo DontDestroyOnLoad 1 Answer

Can you undo "dont destroy on load"? 1 Answer

dontdestroyonload object references 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