SS3D
SS3D copied to clipboard
Consumables script causes the game to crash
Summary
Attempting to utilize a consumable item (such as drinks) results in the game crashing.
Current Behavior
Attempting to drink any of the soda items results in a crash, due to the consumables script creating a null reference error.
Steps to Reproduce
Attempt to drink a soda by picking one up and clicking on yourself.
Expected Behavior
The soda should empty some amount of its contents into the target -- in this case, the player, and play a drinking sound.
Possible Solution
Issue is probably coming from some Network-related shenanigans. The error cites Assets/Engine/Interactions/InteractionSourceNetworkBehaviour.cs:101
as related to the problem. Someone who has experience with the networking code should check this out.
@EikoBiko I think that the issue here is that the itemInHand variable isn't correctly set. It appears to me that the source is always going to be the player who initiates (the John Doe object), who doesn't have a Consumable component attached. The behaviour you have described can be resolved simply be changing the itemInHand assignment to use GetComponentInChildren rather than GetComponent, because the soda can in the players hand is within the John Doe hierarchy.
Relevant code excerpt in Consumable.cs:
protected override void StartDelayed(InteractionEvent interactionEvent)
{
GameObject source = interactionEvent.Source?.GetComponentInTree<Entity>().gameObject;
GameObject target = interactionEvent.Target.GetComponent<Transform>().gameObject;
Consumable itemInHand = source.GetComponent<Consumable>(); // <<<<< This should search children.
// Item in hand and interacting with origin
if (target == null)
{
itemInHand.ConsumeAction(source);
}
// Item not in hand
Consumable targetedItem = target.GetComponent<Consumable>();
if (targetedItem)
{
targetedItem.ConsumeAction(source);
}
// Item in hand and interacting with other player
if (target.GetComponent<Entity>())
{
itemInHand.ConsumeAction(source, target); // <<<<< This is where the error occurs.
}
}