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 /
  • Help Room /
avatar image
0
Question by rgi92 · Feb 25, 2019 at 03:48 PM · scripting problemscripting beginner

(C#) How do I "Do Things" after loading the scene in Unity 2018.2.20???

I've been searching online but methods happen to be outdated or not working in our day Unity version (2018).

I have a character in each scene, so when I enter the trigger on scene A to scene B, I want my character (new character in scene B) to change position. This way, I want to make multiple entrances between scenes without storing the data.

But I have no idea how to since how and where do I assign f.ex. game object that player can use as the destination since previous scene is getting destroyed and stuff... And I have never worked with DontDestroyOnLoad

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 xxmariofer · Feb 25, 2019 at 08:10 PM 0
Share

dont destroy on load is only for persisting gameobjects between scenes. scene 2 will call all the awakes and starts so you could just search for a transform using a tag in the awake event. or why do you need to have the character persist from scene a to b? why cant you find in the scene or maybe use an static claass

1 Reply

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

Answer by AaronBacon · Feb 26, 2019 at 06:01 PM

Well firstly, you dont need to "Work with" DontDestroyOnLoad(), literally just put

 void Awake()
      {
      DontDestroyOnLoad(this);
      }

And the object the script is on will no longer be destroyed on scene change.


Now as for the Multiple entrances, any newly loaded in object will be calling the Start() and Awake() function (any object that wasnt loaded in the previous scene). You can use this to your advantage. The way I would do it is to have a static boolean value on the player or another object that exists across scene change (Using the DontDestoryOnLoad() function) called "loadIn" that is set to false. Then, when you interact with the door in SceneA, it should set the loadIn value to true, then change the scene.


Some object in SceneB should then have the Awake() function checking if loadin == true, (i.e has the player come from a door and needs to be teleported somewhere before loading in). If it finds it is true, it needs to teleport the player.


Now as the player isn't going to be in SceneB, you'll have to use code to find the player gameobject eg:

 private GameObject player;
 
 void Awake()
      {
       //Either:
       GameObject.FindGameObjectWithTag("Player");
        // Find the Player by Tagging it in the Inspector
        
       //Or:
       GameObject.Find("Player1"); 
      // Find by it's name in the scene Object List
      }

After you have the players gameObject, you simply need to tp it to the door in question if loadIn is true. There are a few ways to do this, but how I would do it is to create a doorID on each door in a scene. then give the players script a static int called "goToID". then, when the scene loads in, have every door check first if if the player should load in to a door, and then test if the players goToID, is the same as that doors ID in the scene. Eg:

     public int DoorID;
     public GameObject player;
     private void Awake()
     {
         player = GameObject.FindGameObjectWithTag("Player");
         if (PlayerController.loadIn == true) // "PlayerController" is the nameof the players script.
         {
             if (PlayerController.goToID == DoorID)
             {
                 player.transform = this.transform;
                 //Teleport the player to this object
             }
         }
     }

After that, simply tag the player with "Player" in the editor, then make sure that the part of the Door script that takes the player to the next scene has a line to set both loadIn and and DoorID on the player. Hope this helps, but feel free to ask if you need anything clarified

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

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

NavMesh of an automatically generated maze 0 Answers

Displaying Text on Touch Event 0 Answers

Having trouble moving a GameObject to another scene and Transform it to a different position 0 Answers

unity visual studios not working properly 0 Answers

Finishing up my 2D quiz game [Last Problem] 0 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