I’m working on a multiplayer game in Unity and encountering an issue with parenting NetworkObjects within the scene hierarchy. Here’s the scenario:
- I have a Card prefab that functions as a NetworkObject.
- This prefab contains two child objects: an Image and a Text component.
- To display the card’s visuals, it needs to be parented to the Canvas object (which is a non-NetworkObject).
The Problem:
- When I instantiate and spawn the Card prefab on the network, it appears outside the Canvas in the scene hierarchy, at the same level.
- This results in the card not being displayed in the game scene.
- Attempting to parent the spawned Card object to the Canvas throws an “InvalidParentException”: “Invalid parenting, NetworkObject moved under a non-NetworkObject parent.”
Scene
–Canvas ← (non Network Object)
–Card (clone) ← (Network Object)
–Card (clone) ← (Network Object)
Alternative (But Unclear Solution):
As a workaround, I can nest a separate Canvas object within each instantiated Card prefab. This positions the card visuals correctly within the game scene. However, I’m unsure if this is an optimal or recommended approach.
Card (clone) ← (Network Object)
–Canvas
Card (clone) ← (Network Object)
–Canvas
Questions:
- Is nesting a Canvas object within each Networked Card prefab a good practice in multiplayer games?
- Are there alternative solutions for properly parenting NetworkObjects to a Canvas object while maintaining network functionality?
- Could there be a potential issue with this workaround that I’m overlooking?