This is an experience I had with programming this week. It's a bit of a boring ramble but, what are blogs for...
11:00 AM (AEST), October 12th, 2021
After last weeks post with the working FOV and online play, I decided it was time to wrap my head around RPC calls, and so I figured the random colour selection I wanted for each player would be a great feature to try this out. While it was a great idea to wrap my mind around RPC calls early, I didn't anticipate the amount of time this would take in the end.
So after reading through the documentation for MLAPI and watching a few videos, I thought I had the idea down and tried to implement it. Sadly no ice, no reaction from the code at all. My next thought was maybe it was the actual code for the colour changer. So I coded that for single-player and got it working, put it back on the server and still nothing. Something started to smell fishy, but I thought to myself, it must be something that I've done wrong, so I pushed past that feeling and continued. Back at the drawing board, I thought maybe the NetworkVariables weren't working as I was confident I was writing these RPC calls correctly. I removed all traces of RPC calls and made sure the code functioned from each individual player object instead, and again, it worked. Now I started to get annoyed. I wasn't doing anything wrong.
After a few more days of thinking and reading with nothing to show, I noticed something interesting. The version of MLAPI I was using had the RPC attributes declared as [ServerRPC], but in the documentation and videos, it was [ServerRpc]. I quickly realized it wasn't just [ServerRPC] either, GameObjects were all NetworkedObject instead of NetworkObject, and my synced variables were NetworkedVar instead of NetworkVariable. It turns out this whole time, using autofill to write these terms had hidden the problem right under my nose.
I upgraded Unity, removed the old MLAPI, installed the new MLAPI, spent several hours getting the project functioning again, and finally, it was again time to test my RPC calls. Well, you won't believe this, AGAIN it didn't work. However, there is still hope! I did get a different result this time, and this is the funniest part of programming. Sometimes you are trying to solve an issue, you might go down a path that has nothing to do with the problem, but then that path leads to an actual solution. Well, a Client connecting to the server was now setting the colour of the Host, which meant the RPC calls WERE working in the project, just not as intended. I had a new problem, a hilarious problem with all the added context of the story so far, and that was the RPC calls I was so confident about was wrong.
After some use of paper and actual planning for how I needed this system to work, I managed to wrap my head around what I needed and finally successfully implemented the colour change system. Essentially, the RPC calls were happening correctly but, the NetworkVariable I was using existed on every client, and I was setting that version instead of the server version.