Un able to Instantiate character

Tempus

New member
I need to be able to "Instantiate" my character at run time. But the character and all related scripts do not "link up" and results in the character being un usable.
I also can not set the character to be set as "DO NO DISTROY" either as scene changes require character to load when scene loads.
I did have a related question on this HERE regarding being able to move the character around at runtime.

As such, I managed to get it to drag around and be placed where I like by not having some scripts active at run time.
By some magic, it was working as needed. But I needed to rebuild one of my scenes, and something was lost. No idea what magic setting was used or if was a happy quirk. But now the second part where the character loads and is played is not working.

Some related errors:
NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Game.KinematicObjectManager.Update () (at Assets/Opsive/UltimateCharacterController/Scripts/Game/KinematicObjectManager.cs:839)

NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Camera.CameraControllerHandler.Update () (at Assets/Opsive/UltimateCharacterController/Scripts/Camera/CameraControllerHandler.cs:103)

NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.ThirdPersonController.Camera.ObjectFader.FadeCharacter () (at Assets/Opsive/UltimateCharacterController/Scripts/ThirdPersonController/Camera/ObjectFader.cs:265)
Opsive.UltimateCharacterController.ThirdPersonController.Camera.ObjectFader.Update () (at Assets/Opsive/UltimateCharacterController/Scripts/ThirdPersonController/Camera/ObjectFader.cs:251)

NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Character.CharacterIK.GetDefaultLookAtPosition () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/CharacterIK.cs:372)
Opsive.UltimateCharacterController.Character.CharacterIK.LookAtTarget () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/CharacterIK.cs:758)
Opsive.UltimateCharacterController.Character.CharacterIK.OnAnimatorIK (System.Int32 layerIndex) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/CharacterIK.cs:592)..............................................................

---------------------------------------------------------------------------------------------------------------------------------

I have script in place to reactivate all deactivated scripts when I load the given scene. Some are ones that I deactivated myself for PART A , and the rest are ones that deactivated themselves for some reason related to "NOT FINDING THE CHARACTER" even though it is loaded in ( instantiated from code ) when the scene is loaded.

code01.png

But even though I do all at load, it still fails to work. Only a character already part of the scene works. but that does not help in any way.

Why is it not possible to instantiate a character?
To clarify, the character is placed into the scene as intended in the position and orientation required. the problem is that all the code fails to HOOK together.
 
Last edited:
So, the problem is probably that all the editor references aren't filled out. As far as I know, you have two options.
1. On the line before every null reference exception you need code like this (using your first as an example).

Code:
if(m_KinematicObjectManager is null)
m_KinematicObjectManager = GetComponent<KinematicObjectManager>();

2. Instead of a prefab with every component attached, you spawn the prefab and then run a custom script like the ones from the Tools->Opsive->Character which is CharacterManager.cs I believe.
 
I have needed to move a lot of my code into the On Awake() functions to try and get the code hooks to catch ( and also to not break my code completely in the proses).
This should not be needed and might cause problems later.
so now it is sort of working in stage 2.

Now though I get this error 9 times and then it hooks.

NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Character.CharacterIK.GetDefaultLookAtPosition () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/CharacterIK.cs:372)
Opsive.UltimateCharacterController.Character.CharacterIK.LookAtTarget () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/CharacterIK.cs:758)
Opsive.UltimateCharacterController.Character.CharacterIK.OnAnimatorIK (System.Int32 layerIndex) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/CharacterIK.cs:592)
UnityEngine.Animator:Update(Single)
Opsive.UltimateCharacterController.Character.AnimatorMonitor:SnapAnimator() (at Assets/Opsive/UltimateCharacterController/Scripts/Character/AnimatorMonitor.cs:283)
Opsive.Shared.Events.InvokableAction:Invoke()
Opsive.Shared.Events.EventHandler:ExecuteEvent(Object, String)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion:Start() (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:689)
 
I believe this is due to the character object not having a look source defined yet at that point, so you may need to ensure that it does when instantiating it.
 
The UCC is a well rounded and feature rich controller, but it is looking like it is not compatible with my game.
I am sure that given enough time and persistence that I can get it all to work, but I am not going to rewrite my whole game to do it.
It should within reason stand by itself, but is constantly giving me problems for one reason or another. I am not building a game around the controller.?
 
The UCC is a well rounded and feature rich controller, but it is looking like it is not compatible with my game.
I am sure that given enough time and persistence that I can get it all to work, but I am not going to rewrite my whole game to do it.
It should within reason stand by itself, but is constantly giving me problems for one reason or another. I am not building a game around the controller.?
If you're looking for something that exclusively handles movement, you'd want to look at something like Kinematic Character Controller. UCC does a lot, but that means there's more to learn.
 
I don't mind learning. It is that the way my game is structured and implemented. Like I said, I am not building my game around the controller and I am not going to rewrite most of it because UCC is setup to work in a particular way.
 
A good script for instantiating the character can be found on this page:


In it I use DontDestroyOnLoad to move the character throughout scenes.
 
Thanks Justin. I will have a look, but might be a non starter as I need to instantiate the character at run time as and when the player choses to in one scene. then the character is linked to my save system (Easy Save 3) from that moment on for positioning and loading (unless removed by the player later). I also require the character to NOT have certain scripts active while the player is moving the "character model" around while setting up their levels in the "Edit level scene". But then when in the second scene which is the "play the level" scene, to have all scripts active........ but it is at this point that it falls apart. The UCC seems to need to do a lot of "EARLY" script hooks to be set up. The scenes are affectively blank with just a UI and a floor. Everything loads in after the scene first initialises via my script and Easy Save 3.
I did try to move a lot of my code to the Awake function, but then some of my scene information doesn't load for some reason. The only thing I can try is to stagger the loading. Will mean splitting code up into separate functions.
 
Which scripts/components do you need to have disabled during that initial "setup" phase and why? Depending on what you're specifically trying to achieve, there may be a better way to do it than just disabling/enabling components during runtime, e.g. some kind of state system setup.
 
Observed behaviour.
1: Character and camera are linked. If they don't load in together, they deactivate selected scripts on themselves.
2: In play mode, you can't drag the character around, as when you let it go, it snaps back to it's original position.
3: Reconnecting the character at runtime doesn't seem to work for me, but is possible if run in Awake..... but causes other problems.

I have two parts to the game. First part is where the player can design the level they want. Including placing the character in a given location of their choosing. In this part, the character needs to be "detached" from the camera and be position-able.
Part two is where the level loads and you get to play it.

As in my previous post, I did try to resolve this conundrum, and at first it seemed to be working, but it was affecting how other parts of my code functioned causing errors.

Currently There is only one character, but later there will be others to chose from, with two or more (depending on the player) being in the scene at once being able to switch back and forth between characters during play.

The main problem underlying everything, is that the character is never in the scene ( unless forced ) at scene load time. As well as I can not use "do not destroy on load" as makes no sense for my use case.
 
Top