Null ref error when shooting target

forzabo

Member
Since updating to the latest version UFPS this afternoon (I purchased it only a few weeks ago, so I think this was a minor update), when I shoot at a game object with a simple target script attached, I get this error -- whether I use a projectile or the raycast/ hitscan method:

Code:
NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Events.EventHandler.ExecuteEvent[T1,T2,T3,T4,T5,T6] (System.Object obj, System.String eventName, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) (at Assets/Opsive/UltimateCharacterController/Scripts/Events/EventHandler.cs:605)
Opsive.UltimateCharacterController.Objects.Destructible.OnCollision (System.Nullable`1[T] hit) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/Destructible.cs:217)
Opsive.UltimateCharacterController.Objects.Projectile.OnCollision (System.Nullable`1[T] hit) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/Projectile.cs:64)
Opsive.UltimateCharacterController.Objects.TrajectoryObject.Move (UnityEngine.Vector3& position, UnityEngine.Quaternion rotation) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/TrajectoryObject.cs:542)
Opsive.UltimateCharacterController.Objects.TrajectoryObject.FixedUpdate () (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/TrajectoryObject.cs:465)

FWIW, here's my very simple target script:

C#:
using UnityEngine;
using Opsive.UltimateCharacterController.Events;

public class Target : MonoBehaviour
{
    [SerializeField] private GameObject particles;
    /// <summary>
    /// Initialize the default values.
    /// </summary>
    public void Awake()
    {
        EventHandler.RegisterEvent<float, Vector3, Vector3, GameObject, Collider>(gameObject, "OnObjectImpact", OnImpact);
    }

    /// <summary>
    /// The object has been impacted with another object.
    /// </summary>
    /// <param name="amount">The amount of damage taken.</param>
    /// <param name="position">The position of the damage.</param>
    /// <param name="forceDirection">The direction that the object took damage from.</param>
    /// <param name="attacker">The GameObject that did the damage.</param>
    /// <param name="hitCollider">The Collider that was hit.</param>
    private void OnImpact(float amount, Vector3 position, Vector3 forceDirection, GameObject attacker, Collider hitCollider)
    {
        Debug.Log(name + " impacted by " + attacker + " on collider " + hitCollider + ".");
        Instantiate<GameObject>(particles,transform.position,Quaternion.identity);
        Destroy(gameObject);

    }

    /// <summary>
    /// The GameObject has been destroyed.
    /// </summary>
    public void OnDestroy()
    {
        EventHandler.UnregisterEvent<float, Vector3, Vector3, GameObject, Collider>(gameObject, "OnObjectImpact", OnImpact);
    }
}

I am stumped! Something is obviously not configured ... but what?

any ideas on how to chase this down will be most appreciated.
 
When I tested this I didn't get an exception but the OnObjectImpact callback changed in the most recent version. Take a look at this page for the new event:


Also, this version had a few breaking changes so make sure you take a look at the top part of the release notes:

 
Thanks Justin -- I hadn't noticed the callback changed!

And of course I missed this in the release notes: :eek:

Remove the Opsive/UltimateCharacterController folder before importing.

re-importing now....
 
Ok, reimported, and rewrote target script to conform to the revised callback structure.

BTW -- I beleive there is a typo in the example ("OnRespawn") -- or I am completely dense:

C#:
public void Awake()
    {
        EventHandler.RegisterEvent<float, Vector3, Vector3, GameObject, object, Collider>(gameObject, "OnObjectImpact", OnRespawn);
    }

should be:

C#:
public void Awake()
    {
        EventHandler.RegisterEvent<float, Vector3, Vector3, GameObject, object, Collider>(gameObject, "OnObjectImpact", OnImpact);
    }

But anyway, it's still throwing what seems to be the same error:

Code:
NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Events.EventHandler.ExecuteEvent[T1,T2,T3,T4,T5] (System.Object obj, System.String eventName, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) (at Assets/Opsive/UltimateCharacterController/Scripts/Events/EventHandler.cs:566)
Opsive.UltimateCharacterController.Objects.Destructible.OnCollision (System.Nullable`1[T] hit) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/Destructible.cs:215)
Opsive.UltimateCharacterController.Objects.Projectile.OnCollision (System.Nullable`1[T] hit) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/Projectile.cs:64)
Opsive.UltimateCharacterController.Objects.TrajectoryObject.Move (UnityEngine.Vector3& position, UnityEngine.Quaternion rotation) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/TrajectoryObject.cs:542)
Opsive.UltimateCharacterController.Objects.TrajectoryObject.FixedUpdate () (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/TrajectoryObject.cs:465)

Here's my revised target script:
C#:
using UnityEngine;
using Opsive.UltimateCharacterController.Events;

public class Target : MonoBehaviour
{
    [SerializeField] private GameObject particles;
    /// <summary>
    /// Initialize the default values.
    /// </summary>
    public void Awake()
    {
        EventHandler.RegisterEvent<float, Vector3, Vector3, GameObject, object, Collider>(gameObject, "OnObjectImpact", OnImpact);
    }

    /// <summary>
    /// The object has been impacted with another object.
    /// </summary>
    /// <param name="amount">The amount of damage taken.</param>
    /// <param name="position">The position of the damage.</param>
    /// <param name="forceDirection">The direction that the object took damage from.</param>
    /// <param name="attacker">The GameObject that did the damage.</param>
    /// <param name="attackerObject">The object that did the damage.</param>
    /// <param name="hitCollider">The Collider that was hit.</param>
    private void OnImpact(float amount, Vector3 position, Vector3 forceDirection, GameObject attacker, object attackerObject, Collider hitCollider)
    {
        Debug.Log(name + " impacted by " + attacker + " on collider " + hitCollider + ".");
        Instantiate<GameObject>(particles, transform.position, Quaternion.identity);
        Destroy(gameObject);
    }

    /// <summary>
    /// The GameObject has been destroyed.
    /// </summary>
    public void OnDestroy()
    {
        EventHandler.UnregisterEvent<float, Vector3, Vector3, GameObject, object, Collider>(gameObject, "OnObjectImpact", OnImpact);
    }
}
 
Ok, here's what I've tried, still getting same exception:

1) Created completely new scene with 1 simple cube target
2) whittled target object script down to match the example (except fixing the "OnRespawn" typo)
3) renamed my script "MyTarget.cs" just in case there was some bizarro namespace conflict

I'm officially stumped and out of ideas...
 
Thanks - you're right on the doc. I'll update it.

In terms of the error, you are doing everything correctly and this is a result of still supporting the old callback. I've updated the UCC package and it is being released on the store. As soon as you see the update button in Unity you'll have the fixed version - it looks like all packages are released besides FPC and UCC. You do not need to remove the previous directory with this change.
 
Justin -- thanks!

I was beginning to suspect it was something like this -- I did some poking around in the event handler script, adding some logging to try to see what was going on, and it seems like there was something janky regarding the 5 & 6 parameter handers...

re the "Respawn" typo -- note it appears in at least two places in the docs -- i think the other page that has this example has to do with destructible...
 
Well ... it's not throwing an exception any more, but now my target script/ object is not responding to hits at all.

Can't rule out idiocy on my part, but again, I'm stumped. Attaching scene + non-opsive dependencies in unitypackage.

TIA
 

Attachments

  • janky.unitypackage
    809.2 KB · Views: 1
I have a test for this and it was working when I first changed the event but now it's not. I should have ran it immediately before release.

Ok, for now just revert to the old way of doing things without that object parameter. I'll update the documentation to reflect this and the working version will be in the next release.
 
Top