Forum rules - please read before posting.

Remember Player position

Hi all and Chris.

I was wondering if it's possible to remember the Player's position within a scene.

My scenario:

Open Inventory - Read something which switches to a different scene - use a 'Back Button' to return to the previous scene.

I find that the player will be teleported to a Player Start marker which makes sense. But the Player will be able to read the inventory item at any point, position in any scene. Is there a specific 'Remember' component I should be using?

Many thanks


  • "Remember Transform" will store an object's position, but that probably wouldn't be appropriate here since you wouldn't want it to affect the position at any other time.

    Best way would probably be to rely on player-switching, and reserve a separate "Player" prefab just for the examine scene. If you change scene by switching player, rather than using the "Scene: Switch" Action, the old player's position will be restored automatically.

    See the Manual's "Player switching" chapter for more on this topic.

  • Ah, didn't even consider something like that. Thanks Chris for your super speedy response. I'll revisit that chapter now and let you and everyone know how I get on.

    Thank you :)

  • Ok so I'm obviously doing something wrong as I'm getting an error.

    I've set up a new 'Stand In' Player and assigned it in the Settings (I already allow for player switching)

    To read the inventory item: I tried using the Player Switch 'Appear in other Scene' but that didn't seem to work. So I used Player Switch to my 'stand in' Player.
    I then did a scene change to the close up of the inventory item. Checked the Hierarchy, my new stand in player is present. Good so far.

    I then have a back button, when pressed, it takes you back to the previous scene. In the action list I added another Player Switch back to the original version of the Player. That worked up to a point. A 'Player' appeared in the correct position as where I left the original but as soon as the scene switches I get this error message:

    NullReferenceException: Object reference not set to an instance of an object
    AC.SaveSystem.RemoveAssociatedNPC (AC.Player player, Boolean repositionPlayer) (at Assets/AdventureCreator/Scripts/Save system/SaveSystem.cs:1696)
    AC.SaveSystem.RemoveAssociatedNPCs (Boolean switchingPlayer) (at Assets/AdventureCreator/Scripts/Save system/SaveSystem.cs:1670)
    AC.SaveSystem+c__Iterator1.MoveNext () (at Assets/AdventureCreator/Scripts/Save system/SaveSystem.cs:1656)
    UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

    When looking at the Hierarchy, the NPC version of the Player is present and not the original Player. I already associated the Player with the NPC version (so not the stand in version). I'm also unable to control the player's movement.

    Also when clicking about in the screen after the error message above I get the following error:

    Delaying request to save due to the game currently loading.
    -> AC debug logger
    UnityEngine.Debug:LogWarning(Object, Object)
    AC.ACDebug:LogWarning(Object, Object) (at Assets/AdventureCreator/Scripts/Static/ACDebug.cs:25)
    AC.c__Iterator0:MoveNext() (at Assets/AdventureCreator/Scripts/Save system/SaveSystem.cs:778)

  • The script issue you're getting can be fixed by opening SaveSystem.cs and inserting the following above line 1696:

    if (player == null) return;

    However, the workflow I'd suggest is the same as you first tried, i.e. set the new Player to Appear In Other Scene. In general you should use AC to handle scene changes when player switching is involved - as opposed to doing both separately.

    You mentioned it didn't seem to work. What were the steps you took, exactly, and in what way did it not work?

  • Select the Inventory item 'TT Spell'
    Double click the Examine Action List

    Within action list:

    Camera fade out 0.5
    Set Global variable 'read spell' to True
    Player switch to 'stand in', restore position {yes}, Player, appear in 'new scene', NPC to be replaced {none}
    Old Player remove from scene
    Turn off Inventory Menu

    Within the new scene where we see a close up of the spell and there is a Back button.

    Press back button
    Within the Back Button menu (Button Elements):

    Turn off the Back Button menu
    Unlock InGame menu (a satchel you click to open the Inventory menu)
    Player Switch - to my original and default Player, Restore position {yes}, Replace current Player
    Scene switch previous - overlay current scene {yes}

    So it works the first time. I get a message saying the NPC version of the Player is moved out the way to make room for my original Default player. To note, the stand-in player is not the NPC version.

    So when I go to read (examine) the spell again, it switches to the 'new scene' but my 'Back' button has disappeared. Not sure what to do about that.

  • I also get this message after the NPC version has moved out of the way

    Could not find Resources prefab with ID 342916 - is it placed in a Resources folder?

    -> AC debug logger
    UnityEngine.Debug:LogWarning(Object, Object)
    AC.ACDebug:LogWarning(Object, Object) (at Assets/AdventureCreator/Scripts/Static/ACDebug.cs:25)
    AC.LevelStorage:UnloadTransformData(List`1, SubScene) (at Assets/AdventureCreator/Scripts/Save system/LevelStorage.cs:492)
    AC.LevelStorage:SendDataToScene(SingleLevelData, Boolean, SubScene) (at Assets/AdventureCreator/Scripts/Save system/LevelStorage.cs:177)
    AC.LevelStorage:ReturnCurrentLevelData(Boolean) (at Assets/AdventureCreator/Scripts/Save system/LevelStorage.cs:79)
    AC.SceneSettings:OnStart() (at Assets/AdventureCreator/Scripts/Game engine/SceneSettings.cs:134)
    AC.MultiSceneChecker:Start() (at Assets/AdventureCreator/Scripts/Game engine/MultiSceneChecker.cs:72)

    342916 is my default player prefab which is not currently in my resources folder. Should I move it there?

  • 342916 is my default player prefab which is not currently in my resources folder. Should I move it there?

    This message is showing because of the Remember Transform component attached to your Player, with Save scene prescence? checked. This will conflict with AC's auto-saving of the player's position, so you should remove this if you're not sure.

    So when I go to read (examine) the spell again, it switches to the 'new scene' but my 'Back' button has disappeared. Not sure what to do about that.

    You haven't said how your Back button is told to turn on, so I can't offer anything there. I did, however, find no issue when using the player-switching method - though try setting the Old Player position field to Remove From Scene in both cases.

    Here's an alternative solution: Instead of switching player, create a new PlayerStart in the original scene that's reserved just for the new scene - i.e. when the player returns after this sequence, he'll be at this PlayerStart.

    Then, just before you switch to the new scene, use the Object: Teleport Action to teleport this PlayerStart to the player's current position. If this PlayerStart has the Remember Transform component attached, it'll be saved and so the player should be placed back where he was upon re-entering.

    This would have to be done for all scenes of course, but try it with just one first. Once it's working for one, you can have the ActionList that teleports it work across all scenes by having the "Remember Transform" component share the same Constant ID value across all scenes - see this tutorial.

  • I've managed to sort it. I added a 'turn on menu' for the back button within the 'Examine item' (which runs the Player switch sequence). Tried it numerous times in different scenes and it appears to work every time. This is very handy as I'll need to be able to do this more often further into the game.

    Thanks Chris, you are a star!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.