Mirror icon indicating copy to clipboard operation
Mirror copied to clipboard

Missing Error message when multiple NetworkIdentities are on the same GameObject (due to prefab variants)

Open TheMikina opened this issue 3 years ago • 0 comments

Description I have run into an issue with multiple network identities on a GameObject. While there is a DisallowMultipleComponent tag on the NetworkIdentity script, it's still possible to get multiple identites via inheritance in Prefab variants, in case a NetworkIdentity is added first into the Prefab Variant, and then onto the parent Prefab.

While it's clear from the documentation that this should not work and should never be done, the main issue is that it can happen without you noticing and automatically (more on that bellow), and causes a great deal of trouble that is really hard to debug. The mirror behavior breaks, the scripts are not working or syncing properly, but most importantly no visible errors are shown, so unless you notice that you have multiple identities on an object, there is no way to tell what is happening and why isn't it working. I have rewritten scripts several times and spent a lot of time debugging why are they not working, only to later notice that the issue was in multiple identities on an object.

I think that this issue can also happen automatically, when Mirror/Unity is adding Identities when you add a first NetworkBehavior onto an object. If you add the first behavior to the prefab variant, apply changes to prefab and then later add another one onto the parent prefab (which still does not have any other networked behavior) and apply. I'm fairly certain that I have never added a single Identity manually while in the process of porting a SP game to MP, and I have already run into the issue several times and wasted a lot of time figuring out why the scripts are not working. This, however may have been caused by me adding NetworkTransforms to prefabs and missing that one is a variant of the other, since we do have a kind of messy project (which is entirely my fault).

Repro Steps -> Create a Prefab from Empty Gameobject. -> Create a Prefab Variant from the Prefab -> Add NetworkIdentity to the Prefab Variant and save it. -> Add NetworkIdentity to the Original Prefab -> The Prefab Variant now has two Identities -> Add the Prefab Variant to Networked Scene -> Launch the scene - no Error message is shown, even though the object has two NetworkedIdentities

Expected behavior An error should be thrown into console that the object has two NetworkIdentities, similar to the error message that happens when there is NetworkIdentity in children of another object with NetworkIdentity.

Screenshots image (Red tint is due to being in Play mode, a Scene has been hosted and no errors shown.)

Desktop (please complete the following information):

  • OS: Windows
  • Build target: standalone
  • Unity version: 2020.2.6f1
  • Mirror branch: master

Additional comments I understand that this is more of an Unity issue than a Mirror issue, since the behavior should be prevented by DisallowMultipleComponent, but the check happens anyway in Mirror - for when you have more Identities in children, so there is no overhead added and checking for multiple Identities on the same object is a simple one-liner check.

Including the multiple GO check into my clone of NetworkIdentity code was not a problem, but I created this bug report because it caused me a great deal of trouble and I spent a lot of time debugging and rewriting NetworkBehavior code that was error-less but not working, especially since I don't have much experience with Mirror and was not sure I was writing the NetworkBehaviors correctly, so I am reporting this since it may save someone who just like me does not notice that multiple identities have creeped into his prefab hierarchy. Before I realized what the problem was and what was causing the multiple identities (prefab variants), I have run into the issue several times on separate occasions, with enough time between them that I always forgot to check for multiple identities and spent some time debugging before realizing it (and then finally implementing the check into Mirror).

I understand if this bug is not considered worthwhile as the check would probably be useless for most of the users, so feel free to close this issue. I also don't want to go for PR right away, even though the fix should be easy, since I don't really have any experience with open source development and I'm not sure if there isn't some reason why the check isn't there. But if it is deemed as worthwhile, I can try looking into how to PR properly and implement it.

TheMikina avatar Jul 17 '22 20:07 TheMikina