Errors with monitors after Instantiation

I have a character selection scene that instantiates a character just fine. I have this character set to not destroy on load. I then load the main game scene and have the camera find the character. This works. Things break when trying to setup the crosshairs monitor and slot 0 monitor. After some debugging, I found that Awake is not being called in CrosshairsMonitor.cs at all in this case for some reason. If I put the code that is currently in Awake into OnEnable, then the problem goes away, kind of. I'm not sure if this is the best approach since I'm still learning the UCC stuff. The exception that I am getting is:

NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Utility.UnityEngineUtility.SizeSprite (UnityEngine.Sprite sprite, UnityEngine.RectTransform spriteRectTransform) (at Assets/Opsive/UltimateCharacterController/Scripts/Utility/UnityEngineUtility.cs:106)
Opsive.UltimateCharacterController.UI.CrosshairsMonitor.OnEquipItem (Opsive.UltimateCharacterController.Items.Item item, System.Int32 slotID) (at Assets/Opsive/UltimateCharacterController/Scripts/UI/CrosshairsMonitor.cs:336)

There is a couple more lines to this stack trace, but this is the important part of it.

I found that on line 336 in CrosshairsMonitor.cs, m_CenterRectTransform is the culprit and is null when passed to the SizeSprite method. This is because Awake doesn't get called. I've put a breakpoint in there and it doesn't get hit.

If I put the code that is in Awake in OnEnable this problem goes away. If I try the same fix for the SlotItemMonitor.cs, it doesn't work. Awake is called in that class just fine, so I'm not sure yet what the problem is there.

I'm hoping Justin or someone with some intimate knowledge of UCC can help me out with this. I also want to mention that if I instantiate a character in the main game scene hierarchy then things work just fine. It is only when I have a character instantiated already when I load the game scene, AND the character has a weapon equipped, including the Body item for melee.

I'd also like to mention that if I don't give the character any weapons and instantiate it like I am doing in the previous scene, then things work just fine. I have yet to see if picking up a weapon after this breaks anything, but I don't think it will. It's only when the character starts out with a weapon and is instantiated in the previous screen.

Any help would be greatly appreciated.

Thanks!
 
Last edited:
After you instantiate your character you will need to assign your character to the monitors. There is a Character property that you can assign the GameObject to. I am not sure why Awake isn't being called though - I haven't ever seen that happen and it's on the Unity side of things.
 
The monitors don't exist when I instantiate the character. They are in the game scene which I haven't loaded yet. it's upon loading the game scene that this problem occurs.
 
Ah, in that case Awake not being called is key. I haven't run into this before. I would first update Unity to see if that fixes it and if not create a small repro scene and submit it to Unity - without Awake being called you are going to run into a lot of problems especially if it happens with other scripts.
 
Yeah, I thought it was quite strange that Awake isn't called, but OnEnable is. I will try to reproduce it as minimally as possible. I can see if updating Unity to a newer version will help. I'm using the latest 2020.3 LTS at the moment.
 
I loaded my project up into the latest version of Unity and still have the problem, however, Awake IS being called. It likely was before since I was so tired that I probably didn't have a breakpoint set on the base.Awake method call. With that said, it's failing on line 123 of SlotItemMonitor.cs with this line:

UnityEngineUtility.SizeSprite(m_ItemIcon.sprite, m_ItemRectTransform);

m_ItemRectTransform is null.

The only place I can see this being set is in Awake, but it never hits that portion of the code for some reason. I'm still digging, so I hope that I can get this figured out.
 
Glad you are making progres. The only way ItemRectTransform will be null is if there isn't a RectTransform on the GameObject that the SlotItemMonitor is assigned to.
 
Top