0
Answered

"On Collision Enter" and "On Trigger Enter" in one Graph throws Error

Ingo 3 years ago updated by Lazlo Bonin (Lead Developer) 3 years ago 3

I wanted to extend the OnCollisionWith method to also register a trigger collision aswell.

The graph works with colliding with a collider. If it is colliding with a trigger (changing collider type into trigger at prefab) i get the following error message:


NullReferenceException: Object reference not set to an instance of an object
Bolt.CollisionEvent2D.<Definition>b20_0 (Ludiq.Recursion recursion) (at C:/Users/Lazlo/Projects/Ludiq/Bolt.Flow/Runtime/Flow/Framework/Events/Physics 2D/CollisionEvent2D.cs:45)
Bolt.Unit+<>cDisplayClass56_0`1[UnityEngine.Collider2D].<ValueOutput>b__0
(Ludiq.Recursion recursion) (at
C:/Users/Lazlo/Projects/Ludiq/Bolt.Flow/Runtime/Flow/Unit.cs:264)
Bolt.ValueOutput.GetValue (Ludiq.Recursion recursion) (at
C:/Users/Lazlo/Projects/Ludiq/Bolt.Flow/Runtime/Flow/Ports/ValueOutput.cs:92)

Is it not possible to have to event listener on one graph? I wanted to utilize the one output for both cases.


Thanks a lot!

Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):
GOOD, I'M SATISFIED
Satisfaction mark by Ingo 3 years ago
Answered

It's a bit hard to follow the flow here, but I'll try to explain what's going on.

Basically, you're asking Bolt to fetch the collider from the On Collision Enter event even when it's actually the On Trigger Enter event that got sent.

So that goes:

  1. On Trigger Enter
  2. Branch
  3. Compare Tag
  4. Get collider from On Trigger Enter
  5. Get tag from Input
  6. Branch succeeds, Debug Log
  7. Get message from Select's result
  8. Not Equal check
  9. Get Collider from On Collision Enter
  10. Uh-oh! There's never been an On Collision Enter, hence the error.
    "Collider" is not null when no collision has happened, it's not even defined.

There are many ways to solve this, but the simplest IMO is to set a graph variable with the mode when an event happens, then select based on its value. For example (partial graph):



Hi Lazlo,

thanks a lot for taking the time and answer the question. My goal was to create something that is useable even if the object has a standard collider or a trigger collider. Could it be, that the select Unite is trying to get information from the "On Collider Enter"-Unite which is not existant, and this is firing the error?

I guess I know understand. Thanks again!

Btw. You made a brilliant tutorial. I really appreciate the time and effort you put into it! Lots of fun to learn and follow!

+1

Yes, with my answer you can definitely do what you meant to do!

Basically, replace your middle select, your not equal and your null nodes with the elements in my screenshot, and everything should work fine.