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 staticVoidMan · Dec 24, 2014 at 08:05 AM · gameobjectloadleveldontdestroyonloadawake

DontDestroyOnLoad but script being excuted multiple times

Lets say I have a scene called MainMenuScene in which I have a GameObject, say, Datasource_GO.
On this I've assigned a script, say, Datasource_CS.cs whose contents can be simply:

 void Awake() {
     DontDestroyOnLoad(gameObject);
     Debug.Log("Datasource_CS--Awake");
 }
     
 void Start () {
     Debug.Log("Datasource_CS--Start");
 }

From MainMenuScene I can go to, say, GameScene and then back to MainMenuScene.

The problem here is that with DontDestroyOnLoad I expected Datasource_GO to run the Awake method just once but everytime I load MainMenuScene, it executes Awake.

I believe it's the intended behavior so for now, I have put Datasource_GO in a dummy scene called _DummyScene that loads MainMenuScene with no route back to _DummyScene just to prevent Datasource_GO from executing multiple times.

I would like to know how you guys have/would handle this scenario or have any insight/alternatives to this.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by shriya · Dec 24, 2014 at 10:37 AM

Hi staticVoidMan,

If I understood correctly, your problem is that "Datasource_GO" gameObject is multiplying every time you switch scenes as script has Dont Destroy onLoad method.. For this you can create a prefab of "Datasource_GO" with the script already assigned and instantiate in the first scene i.e your Menu with a check to see if it is already in scene or not.

   if(!GameObject.Find("Datasource_GO"))
     {
     Instantiate (Datasource_Go);//Write correctly yourself
     }

Same is to be done in your GamePlay scene, so that it does not start multiplying in gameplay scene.

I hope I am clear with my point.

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 staticVoidMan · Dec 30, 2014 at 10:37 AM 0
Share

Yes, that's one way but Find is an expensive method and i generally avoid all of them.
I can however do something similar but by using references ins$$anonymous$$d.
Thanks

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Delete duplicate gameobject on restart 1 Answer

DontDestroyOnLoad does not work after reload the scene? 1 Answer

DontDestory gameobject doesn't work on button click after a new scene loads 2 Answers

Loading a level and getting all new gameobjects 1 Answer

How to reference GameObject in Awake or Start 2 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