• 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 zOrcGames · Aug 12, 2019 at 04:18 PM · positionscene-loadingscene-switchingpositioningawake

Awake-Funtion Producing Uneven Results

Hi everyone!

I'm working on an RPG-like Door System where the Doors teleport the player not only to a map, but to a position on a map specified by the recipiant door. In Every Scene, there is a Listener that reads the destination of the player and teleports him there, the player itself has "Doesn't Destroy on on Load" attatched, as well as the Game Manager who saves the name of the door and says it to the Scene Setup Script.


This is the Script of the Scene Listener:

 public class SceneOverview : MonoBehaviour
 {
     string DestinationName;
     GameObject _Player;
     PlayerControlsCC _PC;
     Transform[] Doors;
 
     private void Awake()
     {
         DestinationName = GameManager.Instance._nextDestination;
         _Player = GameManager.Instance.Player1;
         _PC = GameManager.Instance.Player1Control;
         Doors = this.transform.GetChild(0).GetComponentsInChildren<Transform>();
         foreach (Transform T in Doors)
         {
             if (T.gameObject.name == DestinationName)
             {
                 _Player.transform.position = T.GetChild(3).position;
                 //_PC._controller.Move(T.GetChild(3).position);
                 Debug.Log("Transition Time, going to Door " + DestinationName);
                 break;
             }
         }
 
         _PC.CanMove = true;
     }
 
 
 }
 


The Scene "awakens", gets the Destination Name from the Game Manager, gets all necessairy player values and the "Door"-Type objects in the scene, stored in children of the Scene Setup object. If the Name of the "Door" aligns with the name of the Destination, the Player is teleported to the position of the fourth child of the door, which is the position the player should go.


However, sometimes, this script doesn't work at all. Even though the Debug Line prints out the correct line, the player is not positioned at the position of the destination object, instead, presumeably still at the global position it was when the old scene got unloaded.


The Player is moved using a character controller and the .move function in FixedUpdate(), if that might contribute something to this problem. Again, most of the time the script works, but sometimes it completely fails, I'm at a loss. I tried calling the Function on Start() and FixedUpdate() instead of Awake() in hopes of overriding any movement of the player by f.e. the script because I suspected that the movement vector is locked in for the next frame even after the scene transitions, because the player locks the position on FixedUpdate(), but I couldn't prove that yet. I'm at a complete loss, so any help would be appreciated.

Comment
Add comment · Show 2
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 Vega4Life · Aug 12, 2019 at 04:33 PM 0
Share

It's hard to know with certainty what is going on. But, I tend to not rely on other scripts while in the Awake() function. For me, I use Awake to set up the current script I am on, then I use Start() to setup things that rely on other scripts. Doing it this way makes sure that other scripts are also setup correctly - because Awake() happens before any Start() functions.


Beyond that, this one may be hard for us to help track down with what little we have to go off of.

avatar image zOrcGames Vega4Life · Aug 12, 2019 at 04:44 PM 0
Share

I learned about this when reading up on the actual differences between start and awake. $$anonymous$$y current theory is that the FixedUpdate locks in the $$anonymous$$ovement, the Scene gets loaded, the Player gets teleported, then the FixedUpdate is still not over, it moves the player to the position set with CharacterController.$$anonymous$$ove with the values from the last Input, but still like the player were in the old scene, if that makes sense. I‘ll now try waiting for the FixedUpdate frame to end (somehow?) and then transport that player. Is there any info you need that would help you solve this problem?

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

204 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

Related Questions

find whether the spawning space is free or not? 0 Answers

Placing a generated object in the center of a plane.. 1 Answer

Need a Script for Changing Scenes after pressing E to open locked door once a key has been found. 0 Answers

"Scene Scene1 couldn't be loaded because it has not been added to the Build Settings or the AssetBundle has not been loaded" but is in the Build settings 0 Answers

Loading bonus scene and return to previous scene 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