TPS Camera is not rotatable after repeatedly entering a scene - Help

DaboMonser

New member
Hello opsive forum,

I have placed the opsive character, Game, Main Camera game objects into the scene "A", every game object has a "Dont Destroy On Load" script on it.

Now after the first change from scene "A" to scene "B" everything works, then after re-entering scene "A" from scene "B" everything works as well. But after entering the scene "B" from scene "A" a second time the player can control the TPS character, only the TPS camera is not rotatable anymore.

Then these following errors are displayed in the console:

  1. NullReferenceException: Object reference not set to an instance of an object
  2. Opsive.UltimateCharacterController.Game.DeterministicObjectManager.SetCameraRotationInternal (Int32 cameraIndex, Quaternion rotation) (at Assets/Opsive/UltimateCharacterController/Scripts/Game/DeterministicObjectManager.cs:532)
  3. Opsive.UltimateCharacterController.Game.DeterministicObjectManager.SetCameraRotation (Int32 cameraIndex, Quaternion rotation) (at Assets/Opsive/UltimateCharacterController/Scripts/Game/DeterministicObjectManager.cs:522)
  4. Opsive.UltimateCharacterController.Camera.CameraController.PositionImmediately () (at Assets/Opsive/UltimateCharacterController/Scripts/Camera/CameraController.cs:855)
  5. Opsive.UltimateCharacterController.Camera.CameraController.OnImmediateTransformChange (Boolean snapAnimator) (at Assets/Opsive/UltimateCharacterController/Scripts/Camera/CameraController.cs:869)
  6. Opsive.UltimateCharacterController.Events.InvokableAction`1[System.Boolean].Invoke (Boolean arg1) (at Assets/Opsive/UltimateCharacterController/Scripts/Events/InvokableAction.cs:87)
  7. Opsive.UltimateCharacterController.Events.EventHandler.ExecuteEvent[Boolean] (System.Object obj, System.String eventName, Boolean arg1) (at Assets/Opsive/UltimateCharacterController/Scripts/Events/EventHandler.cs:392)
  8. Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.OnEnable () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:566)

  1. NullReferenceException: Object reference not set to an instance of an object
  2. Opsive.UltimateCharacterController.Game.DeterministicObjectManager.SetCameraLookVectorInternal (Int32 cameraIndex, Vector2 lookVector) (at Assets/Opsive/UltimateCharacterController/Scripts/Game/DeterministicObjectManager.cs:492)
  3. Opsive.UltimateCharacterController.Game.DeterministicObjectManager.SetCameraLookVector (Int32 cameraIndex, Vector2 lookVector) (at Assets/Opsive/UltimateCharacterController/Scripts/Game/DeterministicObjectManager.cs:482)
  4. Opsive.UltimateCharacterController.Camera.CameraControllerHandler.FixedUpdate () (at Assets/Opsive/UltimateCharacterController/Scripts/Camera/CameraControllerHandler.cs:130)

  1. NullReferenceException: Object reference not set to an instance of an object
  2. Opsive.UltimateCharacterController.Game.DeterministicObjectManager.SetCameraLookVectorInternal (Int32 cameraIndex, Vector2 lookVector) (at Assets/Opsive/UltimateCharacterController/Scripts/Game/DeterministicObjectManager.cs:492)
  3. Opsive.UltimateCharacterController.Game.DeterministicObjectManager.SetCameraLookVector (Int32 cameraIndex, Vector2 lookVector) (at Assets/Opsive/UltimateCharacterController/Scripts/Game/DeterministicObjectManager.cs:482)
  4. Opsive.UltimateCharacterController.Camera.CameraControllerHandler.OnDisable () (at Assets/Opsive/UltimateCharacterController/Scripts/Camera/CameraControllerHandler.cs:92)

Question: How can I fix this?
 
Last edited:

DaboMonser

New member
The Game GameObject should not be marked as DontDestroyOnLoad. Take a look at this page for an example script: https://opsive.com/support/documentation/ultimate-character-controller/character/
Hey, after i removed the "DontDestroyOnLoad" script from the "Game" gameobject and entered the scene "B" from scene "A" the first time, the same errors mentioned above occurred and again the camera is not rotatable.

How can I make the camera rotatable after changing the scene?
 

DaboMonser

New member
Hey, after i removed the "DontDestroyOnLoad" script from the "Game" gameobject and entered the scene "B" from scene "A" the first time, the same errors mentioned above occurred and again the camera is not rotatable.

How can I make the camera rotatable after changing the scene?
I have also put the "Game" gameobject also into the scene "B", but the above mentioned errors are already displayed in the console and the player can't rotate the TPS camera.

How can I fix this?
 

Andrew

Moderator
Staff member
The errors suggest that your camera cannot find the character in the scene, or that it is attempting to reference a destroyed object. Make sure that in both scenes the camera is able to access the character (e.g. the character's layer is set to Character). Also make sure that each scene works individually, i.e. loading scene A by itself, then loading scene B by itself.
 

DaboMonser

New member
The errors suggest that your camera cannot find the character in the scene, or that it is attempting to reference a destroyed object. Make sure that in both scenes the camera is able to access the character (e.g. the character's layer is set to Character). Also make sure that each scene works individually, i.e. loading scene A by itself, then loading scene B by itself.
So when I take a look after the scene change in the inspector > CameraController Script at runtime, the variable "Character" is assigned by the player gameobject and everything looks fine. The character's layer is set to Character and the scene change is also clean, but the camera is not rotatable and the errors showing up.

What is the best way to let the CameraController script find the player like it should, so that the errors not showing up and the camera is rotatable again?
 
Last edited:

Justin

Administrator
Staff member
Make sure you are first spawning the character/camera from a prefab so you do not get duplicate objects. I have a unit test for this situation so I know that it works if you initially load the objects correctly. The script from the documentation is very similar to the one that I use for the unit test.
 

DaboMonser

New member
Make sure you are first spawning the character/camera from a prefab so you do not get duplicate objects. I have a unit test for this situation so I know that it works if you initially load the objects correctly. The script from the documentation is very similar to the one that I use for the unit test.
I have programmed my own character creator from which the player character gameobject with many other systems attached and the TPS camera gamobject immediately moved to scene "A" via " SceneManager.LoadSceneAsync". I need to rewrite many scripts in the character creator, scene "A" and "B" to work with the player & camera after instantiate the both as prefab into scene "A".

I also tried in a old backup from my project to instantiate the opsive player and camera as prefab into the scene "A", then after entering the scene "B" from scene "A" the same errors displayed in the console and the camera is not rotatable.

I need a way to fix that?

Maybe is there a way to remove the complete opsive camera system clean and then i can use my own?

I'd appreciate it, if you can help me with this.
 

DaboMonser

New member
Make sure you are first spawning the character/camera from a prefab so you do not get duplicate objects. I have a unit test for this situation so I know that it works if you initially load the objects correctly. The script from the documentation is very similar to the one that I use for the unit test.
My own written camera system can find and hold the player GameObject after entering the scene "B" from scene "A" also without a need to instantiate the opsive player and camera as prefab into the scene "A" , only the opsive camera system can't find the player after entering a scene via " SceneManager.LoadSceneAsync".

Can you please tell me how can I let the opsive camera system can work without to instantiate the opsive player and camera as prefab into the start scene?
 

Justin

Administrator
Staff member
Ideally you will spawn the character within the scene at runtime so you don't have duplicate characters/cameras. I think that this is causing the issue.

Once you ensure you only have one instance of each you can then go ahead and reassign the Character field of the CameraController.
 

DaboMonser

New member
This is the script which instantiate the Opsive TPS player "Nolan" and Opsive TPS camera prefabs into the scene "A(Test)" on Awake(), attached to the GamObject named "On_Start":

public GameObject Player;
public GameObject Camera;
public GameObject PlayerPrefab;
public GameObject CameraPrefab;

public GameObject SpawnLocation;

void Awake()
{
if (GameObject.FindGameObjectWithTag("Player") != null)
{
Player = GameObject.FindGameObjectWithTag("Player");
Player.transform.position = SpawnLocation.transform.position;

}
else
{
Player = Instantiate(PlayerPrefab);
Player.transform.position = SpawnLocation.transform.position;
DontDestroyOnLoad(Player);
}

if (GameObject.FindGameObjectWithTag("MainCamera") != null)
{
Camera = GameObject.FindGameObjectWithTag("MainCamera");
Camera.transform.position = SpawnLocation.transform.position;
Camera.GetComponent<Opsive.UltimateCharacterController.Camera.CameraController>().Character = Player.gameObject;

}
else
{
Camera = Instantiate(CameraPrefab);
Camera.transform.position = SpawnLocation.transform.position;
DontDestroyOnLoad(Camera);
Camera.GetComponent<Opsive.UltimateCharacterController.Camera.CameraController>().Character = Player.gameObject;
}
}

Now, after the Player & camera has been instantiated into the scene "A(Test)", the player enters the scene "B(TestTwo)" via the "SceneManager.LoadSceneAsync" methode:

IEnumerator load()
{
yield return null;

AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(Scene_To_Enter);
asyncOperation.allowSceneActivation = false;

while (!asyncOperation.isDone)
{

if (asyncOperation.progress >= 0.9f)
{
asyncOperation.allowSceneActivation = true;
if (shouldSceneBeUnloaded == true)
SceneManager.UnloadSceneAsync(SceneToUnload);
}

yield return null;
}
}

And after entering the scene "B" from scene "A", the player is not moveable.

Here is a short preview:



Did I do something wrong or why is this not working?

I hope this can be fixed, because the opsive tps system is the best tps system available and except this all works perfectly.
 
Last edited:

DaboMonser

New member
Can you send your repro scene to support@opsive.com?
Yes, it would be possible. But I just copied "Nolan" & "Main Camera" from the example obsive scene into my asset folder and spawned it with the first script into the empty "A(Test)" scene.

So you only need "Nolan" & "Main Camera" from your example scene and a empty scene to test if the scene change works for you.

Thanks for your reply and best regards,
- Dabo
 

Justin

Administrator
Staff member
If you send me your unique scripts, scenes, and prefabs in a Unity Package I'll be able to take a closer look at it.
 

Justin

Administrator
Staff member
I took a quick look at your project and it looks like your Enable_On_Start scene is spawning a second character. When I removed Enable_On_Start from your second scene only the original character was spawned and there weren't any errors.
 
Top