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 Coder · Nov 03, 2010 at 05:09 AM · levelsexecute

Run Script From Level to Level

Hi. I'm about 2 weeks new into Unity. This question is similar to this one:

http://answers.unity3d.com/questions/9105/getting-a-script-to-run-without-attaching-it-to-a-gameobject

but not forcing people to read that, suppose I've a game with 2 or more levels. As a player goes from level to level, I need some way to execute the same script at the start of every level. This script isn't going to be seen in-game but just does things like read in data from files. A way I thought of is to basically just create a GameObject, attach my script, and in the Awake method I do:

void Awake()
{
    ...
    DontDestroyOnLoad(transform.gameObject);
    ...
}

so that way, my GameObject persists from level to level, which contains my actual script that I need to run from level to level. Is this a decent way? Seems a bit of a hack because the problem with the above is that if I load the same level n number of times, it creates n instances of the GameObject, so I'd have to do a check for that, so that is why this seems a bit of a hack to do it like this. Another way I thought of is to merely statically create a GameObject for every level and attach my script, but that's unelegant, but it seems better, so as to avoid a possible memory leak.

Comment
Add comment · Show 3
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 lampshade · Nov 03, 2010 at 05:21 AM 0
Share

have you tried using DDOL(this); ? in Awake()?

avatar image Coder · Nov 04, 2010 at 05:21 AM 0
Share

Hi. Not sure what DDOL is?

avatar image Soviut · Nov 25, 2010 at 09:52 AM 0
Share

DDOL is DontDestroyOnLoad. He was asking if you've tired DontDestroyOnLoad(this)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by zannghast · Nov 03, 2010 at 05:38 AM

What you could do is load the gameObject in a scene that is less likely to be "loaded again". Say for example the start-up screen. Then call DontDestroyOnLoad on that object in said scene.

Alternatively, have you tried experimenting with singletons?

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 Coder · Nov 04, 2010 at 05:42 AM 0
Share

Hi. Thanks for the input. Hadn't thought of loading it up on a start-up screen; sounds like a great idea; it's probably what I'll do. I like the Singleton suggestion, but if it'll still require the creation of a GameObject for every level solely for containing a script or maintain a persistent GameObject across levels, then I think your 1st suggestion is preferred for my needs. Thanks for the input, and I'm sure I'll have lots of questions about other things.

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

No one has followed this question yet.

Related Questions

Possible to execute a script before Editor close? 3 Answers

script gets started two times 1 Answer

Stop Script Completely. Doesn't Execute Another Line Of Code. 1 Answer

Different actions with same script called on differend objects 2 Answers

Script problems 1 Answer


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