Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 bublebboy · Mar 16, 2010 at 07:35 PM · spawningdontdestroyonload

DontDestroyOnLoad() does not seem to be working.

I have an empty game object with a script attached to it which I intend to use to keep track of variables between level loads. I am using it to keep track of checkpoints. When playing the checkpoint is updated in the gameObject, but when the level is loaded the variables I changed are reset. Here is my code:

var CurrentSpawn : GameObject;

function Start() { CurrentSpawn.SendMessage("SpawnCharacter"); }

function Awake() { DontDestroyOnLoad(this); }

function Update() { print(CurrentSpawn.transform.name); }

A different function changes the spawn point and that part is working, it is just that the spawn point is reset to the original when the level is re-loaded.

Any assistance would greatly be appreciated.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by duck · Mar 16, 2010 at 08:09 PM

That's probably because "CurrentSpawn" references a different object which is destroyed on load. When the new re-set version of the level comes around, that same spawnpoint gameobject is probably considered - by Unity - to be an entirely different new object.

Perhaps instead of storing a reference to that object, you should just store the position of the spawnpoint as a Vector3 (and also the rotation as a Quaternion, if that is required). Because Vector3's and Quaternions are structs, your script will retain its own copy of the values, rather than referencing some other object which gets flushed away when the Load occurs.

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 bublebboy · Mar 16, 2010 at 11:16 PM 0
Share

Thank you very much for the information! I was able to use it to get my checkpoints working!

avatar image
1

Answer by Jaap Kreijkamp · Mar 17, 2010 at 12:02 AM

If you want to keep CurrentSpawn alive as well, you need to tell it not to destroy as well, by the way, to differentiate between vars and functions and objects its practice to start a variable with lowercase:

var currentSpawn : GameObject;

function Start() { currentSpawn.SendMessage("SpawnCharacter"); }

function Awake() { DontDestroyOnLoad(this); DontDestroyOnLoad(currentSpawn); }

function Update() { print(currentSpawn.transform.name); }

Comment
Add comment · 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
0

Answer by Cyclops · Mar 16, 2010 at 08:28 PM

If an example would help, I posted an Answer in another Question:
How to Keep Information when Reloading a Level.
Which basically points to a demo package I wrote, with code, at:
Simple Demo of a Level Manager

Comment
Add comment · 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

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

Don't Destroy on Load Function is not working for me, and... 2 Answers

Random object spawn? 1 Answer

Spawning objects from moving objects, In C# 1 Answer

Changing spawning position with C# 2 Answers

Stop spawning on bool false and enable on true? 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