Monday, March 1, 2021

Unity Namespaces - passing variables

In a unity project I was working on, I was having a heck of a time passing variables between a 3rd party plugin I'd downloaded and was splicing into (ROS Sharp) because I was trying to make some custom ROS messages. The problem is a default unity project script doesn't have a default namespace, so I defined some public static variables in the 3rd party plugin scripts where I needed to handle my custom message. defining some get/set methods enabled me to access it from my project (which others had touched and was filled with a cluttered set of related scripts that I really didn't want to try and encapsulate in its own namespace.)

Anyway the solution was found here:
https://stackoverflow.com/questions/740937/accessing-variables-from-other-namespaces


Key Lessons for Unity Projects:
  • Own Namespace (best practice) - Probably good practice to change default unity project script with its own namespace. This is probably good so you don't have conflicts when you bring in 3rd party assets and c# plugins. Probably annoying to setup, but i can imagine this helping tons in big multi-user projects (like the one i'm working on)
  • static variables - sort of like a global variable that shouldn't change much, and here's the kicker - can be accessed everywhere. "A static class cannot be instantiated. All members of a static class are static and are accessed via the class name directly, without creating an instance of the class." This is handy because I don't want to instantiate the class a million times.


PS - I need to play around with https://catlikecoding.com/unity/tutorials/flow/texture-distortion/




No comments:

Post a Comment