pilky / twitchmata

A set of files to help simplify integrating Twitch with an Animata VIPO stream built in Unity
MIT License
11 stars 3 forks source link

Fix feature managers getting registered twice #55

Closed megalon closed 1 year ago

megalon commented 1 year ago

Problem

All feature managers are currently registered twice on startup.

This can cause Twitchmata to throw errors about duplicate items, such as trying to create 2 of each managed reward.

[TWITCHMATA] Could not create managed reward. Make sure a reward with this name doesn't already exist.
If you wish to convert an existing reward to a managed reward you must first delete the reward in Twitch's dashboard.

Cause

ConnectionManager.RegisterFeatureManager(FeatureManager manager) gets called twice for each FeatureManager.

The culprit here is GetComponents and GetComponentsInChildren being called on the same GameObject right after one another, which returns the same components twice.

Confusingly, GetComponentsInChildren returns the components on the children and also the object itself.

From the Unity docs

Gets references to all components of type T on the same GameObject as the component specified, and any child of the GameObject.

Solution

The solution here is to simply remove the call to GetComponents and only use GetComponentsInChildren.

I've tested this in my own project and it works fine for me.

If only Unity had named it GetComponentsInSelfAndChildren

pilky commented 1 year ago

@megalon Thanks very much for finding this and for creating the PR. This is all merged and will be in the next Twitchmata release