How to store and access variables in game objects? (coming from gamemaker)

Noob here.

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)

thanks

You ensure the field is serialized, then you can edit in a components inspector: Unity - Scripting API: SerializeField

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.

1 Like

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.

1 Like

I appreciate it. I have to just jump in sometimes way over my head. But I love it. Unity is stupid powerful :exploding_head::exploding_head::exploding_head:

1 Like

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:

For the highest score: Unity High score tracking - Pastebin.com

Usage:

TheBest.RecordScoreIfHigher( lastGamePlayScore);

For the lowest time: Unity best time tracking - Pastebin.com

Usage:

TheBest.RecordTimeIfLower( lastGamePlayTime);

To retrieve the best score or time, use one of these:

int bestScore = TheBest.BestScore;

float bestTime = TheBest.BestTime;

Full usage example:

1 Like

For anyone in the future:

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:

Datasacks is presently hosted at these locations:

https://bitbucket.org/kurtdekker/datasacks

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:

"com.unity.ugui": "1.0.0",
1 Like