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 PijusVose · Jul 04, 2017 at 12:15 PM · scenescene-loadingreloadingreferences

Keeping reference after scene reload

I am making a simple Pong game on IPhone. I have made a Singleton GameManager, which keeps references to the Score text, Game over panel and the ball. Whenever I reload my scene, all of those references are missing. How do I keep those gameobject references after reload?

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
0

Answer by efeguclu · Jul 04, 2017 at 01:51 PM

Use DontDestroyOnLoad

 private void Start(){
 DontDestroyOnLoad(gameObject);
 }
 // or JS
 
 private function Start(){
 DontDestroyOnLoad(gameObject);
 }


or that may be better..

 private bool IsDontDestroy=false;
 
 private void Start(){
 IsDontDestroy = true;
 }
 private void OnLevelWasLoaded(int level){
 if(!IsDontDestroy)
 Destroy(gameObject);
 }
 private void DisableDontDestroy(){
 IsDontDestroy = false;
 }

or JS

 private var IsDontDestroy=false;
 
 private function Start(){
 IsDontDestroy = true;
 }
 private function OnLevelWasLoaded(var level){
 if(!IsDontDestroy)
 Destroy(gameObject);
 }
 private function DisableDontDestroy(){
 IsDontDestroy = false;
 }

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 PijusVose · Jul 04, 2017 at 02:59 PM

What if I want those objects become destroyable after load? How do I Undo DontDestroyOnLoad?

Comment
Add comment · Show 7 · 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 efeguclu · Jul 04, 2017 at 03:02 PM -1
Share

simply use gameObject.Destroy();

avatar image FunIsDangerous · Jul 04, 2017 at 03:05 PM 0
Share

DontDestroyOnLoad does exactly what it says it does. It won't destroy the object when a new scene is loaded. Doesn't make it undestructable programmatically

avatar image efeguclu FunIsDangerous · Jul 04, 2017 at 03:10 PM 0
Share

no it's possible check my answer..

avatar image FunIsDangerous efeguclu · Jul 04, 2017 at 03:20 PM 0
Share

read what I wrote carefully :p

Show more comments
avatar image PijusVose · Jul 04, 2017 at 04:25 PM 0
Share

But when the object is on DontdestroyOnLoad, it is not in the scene, but in the Dont Destroy On Load in the Hierachy. How do I put it back into my Scene in the hierarchy?

avatar image efeguclu PijusVose · Jul 04, 2017 at 05:15 PM 0
Share

if it is UI object use the OnLevelWasLoaded Code that I gave and Add these lines

 gameObject.transform.SetParent(GameObject.Find("Canvas").transform);

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

76 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 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 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 avatar image

Related Questions

Changing scene causes null reference exceptions 3 Answers

Unity Oculus VR: objects that are held when reloading the scene can not be picked up in the next scene 0 Answers

Reloading current scene make game laggy? 0 Answers

How can i save an entire scene when a checkpoint is hit 1 Answer

Classic Resident Evil-style room loading/level streaming? 4 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