In gamemaker I could go into an objects create even and fill it up with variables like
attackDamage = 10;
And then I could use that throughout the game like
health -= other.attackDamage;
Can someone point me in the right direction- whats the best way to store lots of variables that pertain to an object? (and access them from other objects)
Then you can modify the value within its own instance, or via other instances that have gained a reference given the values are public, or you expose methods to to do.
But is is pretty much like, lesson 1 Unity tutorial stuff. Any beginner tutorials would be covering this. Such as Unity’s own programmer course: Junior Programmer Pathway - Unity Learn
Your time would be better spent doing some guided learning to cover these basics.
ok thanks.
ok so like a maybe a script called “InitializeCharacterStats” and add it to the appropriate objects? I just was thinking there might be a better way
Components are a core part of Unity workflow, as is referencing other objects via the inspector (be it components or any other type of object). References can also be established at runtime in a number of ways.
Not to mention that Unity uses C#, so this follows proper OOP principles which I imagine is different from game-maker which from memory is more your typical lightweight scripting language that forgoes a lot of that.
For immutable data you may want to look at scriptable objects, which are how you design your own custom assets: Unity - Manual: ScriptableObject
Again, I suggest doing some proper guided learning of Unity. It’s no doubt very different to something like game maker.
DO IT! And welcome… jump right in, the water is fine.
Also, don’t forget about static variables, which are the closest thing to a global variable, global everywhere.
They get a bad rap when misused but if you’re making a fun little game for yourself and to learn, staticvariables are a great simple way to move forward.
But they do have some gotchas, such as you can’t serialize them or see them in Unity inspectors. You need to have instance variables for that.
But for some ultra simple stuff like your score or lives or something, why not?
Here’s something to get you off the ground:
Tracking simple high score / low time single-entry leaderboard:
this video has given me a pretty good sense of direction on how to setup a system of many variables and stats across many characters/ units:
He uses a system of scriptable objects and dictionaries. He has each unit (character) instantiate a copy of the scriptable object when the unit is created. So hes using the scriptable object as a template.
ScriptableObjects are awesome. Many uses, including predefined data, even runtime temp data if you must, etc. In fact, I even based my Datasacks package around ScriptableObjects.
Here’s how Datasacks work… they’re basically bindable global statics:
If you are using any version of Unity later than Unity2021, it may be necessary to add this line to your Packages/manifest.json, or add it via the Package Mangler: