Tips / Gotchas

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”[1].

I’ll also try to include any helpful tips I come across.


  • You cannot parent two Rigidbodies together[2].
    • Use a FixedJoint or similar component instead[3].
  • 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[4].
  • 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[5].
  • 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.

Network Gotchas

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 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, 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.
Podiatry Clipboard
[1] I don’t really mean edge cases as such, since a lot of this stuff comes up whenever you use a particular feature. But most people may never use it.
[2] Well, you can, but it goes haywire. I really don’t know why Unity’s physics engine can’t handle this, and honestly it’s really annoying.
[3] But even this won’t work well on fast-moving objects; the joint will actually jitter around a lot instead of being truly fixed.
[4] I.e. if you do SomeClass.someEvent += MyClass.someFunction in Awake(), Start() or OnEnable(), do SomeClass.someEvent -= MyClass.someFunction in OnDisable() or similar. Otherwise the garbage collector will not destroy MyClass when you expect since there’s still a reference to it in SomeClass.
[5] Don’t worry about it until you need to. As you may already know, premature optimisation is the root of all evil.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s