Error sending Message

Announcements, support questions, and discussion for Quest Machine.
Post Reply
GorkaGames
Posts: 178
Joined: Fri Sep 21, 2018 8:38 pm

Error sending Message

Post by GorkaGames »

Sometimes I get this error sending a message:
But the odd thing is that I receive the message so I don't understand why the error is launching:
Any idea?

Message System exception sending 'ActivateChest'/'ChestTest' to Quest Chest Parent (ChestInteractableParent): Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
UnityEngine.Debug:LogError(Object)
PixelCrushers.MessageSystem:SendMessageWithTarget(Object, Object, String, String, Object[]) (at Assets/Plugins/Pixel Crushers/Common/Scripts/Message System/MessageSystem.cs:325)
PixelCrushers.MessageSystem:SendMessage(Object, String, String, Object[]) (at Assets/Plugins/Pixel Crushers/Common/Scripts/Message System/MessageSystem.cs:385)
PixelCrushers.QuestMachine.ChestQuestAction:Execute() (at Assets/Scripts/Quests/My Quest Nodes/ChestQuestAction.cs:37)
PixelCrushers.QuestMachine.QuestNode:SetState(QuestNodeState, Boolean) (at Assets/Plugins/Pixel Crushers/Quest Machine/Scripts/Quest/Quest Node/QuestNode.cs:423)
PixelCrushers.QuestMachine.QuestNode:OnParentStateChange(QuestNode) (at Assets/Plugins/Pixel Crushers/Quest Machine/Scripts/Quest/Quest Node/QuestNode.cs:512)
PixelCrushers.QuestMachine.QuestNode:SetState(QuestNodeState, Boolean) (at Assets/Plugins/Pixel Crushers/Quest Machine/Scripts/Quest/Quest Node/QuestNode.cs:431)
PixelCrushers.QuestMachine.QuestNode:OnConditionsTrue() (at Assets/Plugins/Pixel Crushers/Quest Machine/Scripts/Quest/Quest Node/QuestNode.cs:501)
PixelCrushers.QuestMachine.QuestConditionSet:SetTrue() (at Assets/Plugins/Pixel Crushers/Quest Machine/Scripts/Quest/Quest Condition Set/QuestConditionSet.cs:178)
PixelCrushers.QuestMachine.QuestConditionSet:OnTrueCondition() (at Assets/Plugins/Pixel Crushers/Quest Machine/Scripts/Quest/Quest Condition Set/QuestConditionSet.cs:173)
PixelCrushers.QuestMachine.QuestCondition:SetTrue() (at Assets/Plugins/Pixel Crushers/Quest Machine/Scripts/Quest/Quest Subasset/Quest Condition/QuestCondition.cs:63)
PixelCrushers.QuestMachine.LocationToReachQuestCondition:OnMessage(MessageArgs) (at Assets/Scripts/Quests/My Quest Nodes/LocationToReachQuestCondition.cs:40)
PixelCrushers.MessageSystem:SendMessageWithTarget(Object, Object, String, String, Object[]) (at Assets/Plugins/Pixel Crushers/Common/Scripts/Message System/MessageSystem.cs:321)
PixelCrushers.MessageSystem:SendMessage(Object, String, String, Object[]) (at Assets/Plugins/Pixel Crushers/Common/Scripts/Message System/MessageSystem.cs:385)
LocationSpotQuest:SpotReached() (at Assets/Scripts/LocationSpotQuest.cs:131)
LocationSpotQuest:Interact() (at Assets/Scripts/LocationSpotQuest.cs:101)
Opsive.UltimateCharacterController.Traits.Interactable:Interact() (at Assets/Opsive/UltimateCharacterController/Scripts/Traits/Interactable.cs:74)
Opsive.UltimateCharacterController.Character.Abilities.Interact:DoInteract() (at Assets/Opsive/UltimateCharacterController/Scripts/Character/Abilities/Interact.cs:204)
Opsive.UltimateCharacterController.Game.ScheduledEvent:Invoke() (at Assets/Opsive/UltimateCharacterController/Scripts/Game/Scheduler.cs:80)
Opsive.UltimateCharacterController.Game.Scheduler:Invoke(ScheduledEventBase, Int32) (at Assets/Opsive/UltimateCharacterController/Scripts/Game/Scheduler.cs:657)
Opsive.UltimateCharacterController.Game.Scheduler:FixedUpdate() (at Assets/Opsive/UltimateCharacterController/Scripts/Game/Scheduler.cs:329)
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Error sending Message

Post by Tony Li »

This means some code in Quest Chest Parent's OnMessage() method had this error:

Index was out of range. Must be non-negative and less than the size of the collection.

The Message System traps errors because Quest Machine needs to continue functioning even if a listener has an error. Unfortunately this means you can't double-click on the error in the Console window and go to the actual error line in your script.

Look in your OnMessage() method for any code that accesses an element of an array or list. Make sure the index is valid. For example, instead of:

Code: Select all

var someElement = myList[index];
do something like this:

Code: Select all

if (0 <= index && index < myList.Count)
{
    var someElement = myList[index];
}
else
{
    Debug.LogError("Index " + index + " is not valid. The list has " + myList.Count + " elements.");    
}
GorkaGames
Posts: 178
Joined: Fri Sep 21, 2018 8:38 pm

Re: Error sending Message

Post by GorkaGames »

Yes, you are right, that was the problem, Thanks.

One more thing, to suscribe to 2 events on the same script, is this done on the right way or is better for performance to do it on a single listener? If so, how do I write it?

void OnEnable()
{
//Nos suscribimos a los mensajes de Quest Machine
MessageSystem.AddListener(this, QuestMessageToActivate, QuestParameterMessage);
MessageSystem.AddListener(this, QuestMessageToDeativated, QuestParameterMessage);
}
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Error sending Message

Post by Tony Li »

That's the correct way.
Post Reply