script should check which object it is attached to. help please!

Let’s say I have 5 Buildings. They were saved automatically generated with numbers. So I have unique objectnames and playerprefs for them now.

Objectname | Playerpref
Object1 | ObjectID1
Object2 | ObjectID2
Object3 | ObjectID3

Every object has a plain managementscript attached for example to raise the level.

After several headaches I don’t find a way to establish a connection between the objectname it’s playerpref.

I can’t force the script to “know” which object it is attached at, to call the playerpref.

Any Ideas?

the property ‘this.gameObject.name’ would be the name of the gameObject in the scene this script is attached to. If that’s the ‘id’ you’ve associated with in the PlayerPrefs, that’s the preference id you pass in.

I’ll try out to pass that name into the called PlayerPref. Ty for helping out.

Your PlayerPrefs are just a hashtable/dictionary that associates a string with a value. You said that the string name of each pref is that of the gameObject name, and is unique. So you just get the value passing in the gameObject’s name.

var val = PlayerPrefs.GetString(this.gameObject.name);

Unless this ISN’T how you have it set up… in which case, well I can’t help you unless I know how you have it set up. This is YOUR design, not a standard design.

If you have a situation where you need to extract parts of two strings and compare them, you might want to look into regular expressions. They are a bit tricky at first, but they are incredibly flexible once you get used to them. Unfortunately, the topic is a bit large for a single forum post, so i’d suggest googling a good tutorial.

Everything fine and working.

lord: You are right. My design sure is weird but it’s the easiest way for now to seperate each building from another. :wink:

chaj: at the moment it’s enough to search my Perfs that way and I separated the “level upgrade” in an own string. Later I’ll try to compare stuff.