Hey all! This is my first post here so I’ll give a quick background:
I know a bit of C++ from learning it the past few months and am getting into learning Unity and C# (I figger it’s a better fit since I’m reasonably good with C++).
In programming C++ - I would often create a mess of functions particular to a single aspect of a game for tidyness… ex: keep my inventory management functions in one script, combat functions in another, public variables or random functions which I can call as needed from anywhere, so on and so forth.
I did this to basically keep things nice and tidy in my code so I wasn’t digging through single scripts thousands of lines to find what I needed.
Can I do the same with Unity? Have a separate script which is NOT attached to a game object and call functions or public variables from within?
Example: Damage calculator function located in combat_scripts.cs (or whatever I want to name it).
Perhaps I want to use this function in multiple scenarios and is not indicative to a single object either player, enemy or random world damage… basically… wherever I want to use this math function - it is available.
Do I have to attach the “combat_script.cs” to an object in order to reference functions from within the script? Or can the script merely be part of my project assets and I can call functions or variables from within that script whenever I want?
I’m assuming removing the Monobehavior since it won’t be attached to an object?
I’ve only been “mucking” around with unity and C# for about 3 days and having a blast. Learning Unity3D through the book “Unity 3.x Game Development Essentials” (gets my high five!) and C# through various books here.
I always like to “stray” a bit from the books - as I was learning C++ I would often stop at a particular chapter and mess around building stuff with what I’ve learned - testing - researching and trying different approaches - makes the learning process more fun for me.
I would like to do the same with Unity3D - as I write scripts particular to this book I’d like to “experiment” with different approaches - try things the long way - try things the short way - look for ways to optimize code, etc.
I just flat-out love toying around and wasn’t sure how Unity3D or C# would work with respect to calling functions from other scripts not attached to objects. I know you can call functions or get variables or whatever you need from scripts attached to objects and that is cool.
But I was just curious as I like having global or public functions I can use scene after scene even without the same object being used in a different scene.
Anyhow, I appreciate any thoughts, suggestions, comments and high fives to this newcomer.
Sorry for the long post!
Thanks again in advance!
- Jack