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
5
Question by BinaryCaveman · Aug 02, 2010 at 08:38 PM · scenefunctionmonobehaviourscene-change

Function triggered when scene changes?

Is there a function or MonoBehaviour in Unity which is triggered before a scene changes in the scene that is being changed from? (such as when Application.LoadLevel() is called) Something like the opposite of the Awake() function?

Thank You!

Comment
Add comment · Show 1
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 John 20 · Nov 12, 2010 at 06:34 AM 0
Share

hey you commented on $$anonymous$$e so...

well I got the gun to show but now I need it to move with the camera. Also I kinda need crosshair help.

4 Replies

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

Answer by Mike 3 · Aug 02, 2010 at 08:47 PM

There is an OnLevelWasLoaded(int : level) function, which should do exactly what you want

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 BinaryCaveman · Aug 02, 2010 at 08:49 PM 2
Share

Sorry - I should rephrase my question. I want to know of a function that is called before a scene changes, in the scene the user is changing from.

avatar image Mike 3 · Aug 02, 2010 at 08:51 PM 1
Share

Then no, there isn't one. You'll get an OnDisabled, but that's about it - The problem is that it's fired when you disable the script too (or make the gameobject inactive)

avatar image BinaryCaveman · Aug 02, 2010 at 08:51 PM 0
Share

O$$anonymous$$, then. Thank You!

avatar image AndyMartin458 · Sep 21, 2016 at 03:23 PM 0
Share

This is no longer the preferred way to do this. Check my answer for Unity5.4.

avatar image
15

Answer by AndyMartin458 · Sep 21, 2016 at 03:20 PM

In Unity5.4 (and maybe 5.3?), OnLevelWasLoaded is no longer listed in the documentation. MonoBehaviour documentation

You need to subscribe to the event SceneManager.activeSceneChanged instead. SceneManager-activeSceneChanged documentation

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
6

Answer by dil33pm · Nov 23, 2015 at 03:10 PM

You can create a dummy gameObject and write your code in its onDestroy() function. When the scene changes, onDestroy() will be called and your code will be executed.

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
-1

Answer by tinachanmakes · May 18, 2019 at 06:21 AM

I used this to reposition my player back at its original position whenever a scene changed. It would detect a change through the update function.

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using UnityEngine.SceneManagement;
     
     public class Resetter : MonoBehaviour
     {
         Vector3 originalPos;
         Scene m_Scene;
         Scene f_Scene;
     
         void Start()
         {
             m_Scene = SceneManager.GetActiveScene();
             originalPos = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
         }
     
         // Update is called once per frame
         void Update()
         {
             m_Scene = SceneManager.GetActiveScene();
             if (m_Scene.buildIndex != f_Scene.buildIndex)
             {
                transform.position = originalPos;
             }
             f_Scene = SceneManager.GetActiveScene();
         }
     }
 
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 AndyMartin458 · May 18, 2019 at 01:52 PM 0
Share

You’re doing additional computation every update. It’s much more efficient to subscribe to activeSceneChanged.

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

SerializedObject target has been destroyed. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) 9 Answers

Re-loading a scene but on the background older scenes are displayed 1 Answer

"Main" function? 3 Answers

Problem with Save Scene 1 Answer

UnloadSceneAsync() does not seem to work with additive scenes. 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