Unity is generally pretty good, but occasionally a feature will work totally differently to your expectations. This page catalogues some of these “gotchas” as I run across and solve them, to hopefully save someone some face-desk interaction.
There’s already a nice list here. You should check them out first, because I won’t be repeating them here. Those also tend to be more basic and general, while mine are more about “edge cases”.
I’ll also try to include any helpful tips I come across.
- You cannot parent two Rigidbodies together.
- Use a FixedJoint or similar component instead.
- To hide the cursor and keep it inside the game, use Screen.lockCursor = true every frame.
- If you’re using events (you probably should be) make sure you unlisten to them when appropriate.
- You cannot use new on a MonoBehavior. You can only use AddComponent or Instantiate.
- References built in to MonoBehaviour to standard components like transform, rigidbody etc are not actually references. They are properties whose get function calls GetComponent(). So if you use something a lot, cache it with a real reference if you’re worried about performance.
- Layers are bitmasks, not integers. So instead of 00010000 meaning layer 16, it means layer 4. Since layer is an int32, you only have 32 layers (0 to 31). I’ll probably do a post about this.
- If your main character uses a Rigidbody, you should probably set the interpolation setting to Interpolate.
- Physics updates don’t sync with framerate, so you may experience jitter. Interpolation fixes this.
Yes, I feel there’s a need for a separate Network section. Because oh my god. Quick note, this is about Unity’s built-in networking only.
- Network.Instantiate is buffered. Network.Destroy is not. You must use Network.RemoveRPCs before Network.Destroy. This will remove all buffered RPCs, including Network.Instantiate.
- If you have two scripts on one gameObject with identically-named [RPC] functions, you cannot tell networkView.RPC which one to use. It’ll use one and not tell you it didn’t use the other. RPCs should be uniquely named even in different scripts!
- Network groups don’t do what you think. I have no fucking idea what they do do.
- Using Network.Instantiate(prefab, position, rotation, int group) does not set that prefab’s networkView.group to group.
- Using Network.SetReceivingEnabled(player, int group, false) just doesn’t seem to work (RPCs are still received even when I’ve manually set the networkView.group), so it’s not doing whatever I’m trying to use it to do.
- The only thing I know is that it’s used as a bitmask, not a true int32.