Generate a variable for each object that collides

Hi, im new to the forum, i logged in to ask a question… Actually Im developing a project where i need to generate and share a variable (a float) between two gameobjects, like a friendship on Sims (not so complex) or similar videogames. The problem is that after a long time I can not see a way to do it.

For now, what I have is that when the player collides with another object, it’s been added to a list of “Contacts”. My closer idea is to generate a variable in order for each gameobject who is in to this list. The problem is that I find no way to generate this variable.

I hope to solve this, and thanks for any help :smiley:

For both objects to access the shared relationship formed after a collision, both of them could keep a Dictionary of object/friendliness.

Dictionary<GameObject, float> friends = new Dictionary<GameObject, float>();

void OnCollisionEnter(Collision collision)
    if (friends.Contains(collision.gameObject))
        // I already know this dude. He's my friend. Let's be more friends.
        friends[collision.gameObject] += 1f;
        // A new dude. Dunno if I like him. Ok, yes I do.
        friends[collision.gameObject] = 1f;

However this approach each friend has a connection that is private to one another. For example, the green sphere and the yellow sphere could both know each other with 3 friendliness to each other.

But, yellow can modify their friendliness to green, so yellow thinks green is 10 friendly while green thinks yellow is 3 friendly. If you want them both to share the same weight, you should let another class deal with the relationship. It’s not so much of a problem if you only update the friendliness on contact because both will do the same action on each side so the effect is mirrored, but if one decides to bump up the friendliness after another action that doesn’t involve the other, then the two will have different stances to one another.

Consider implementing a class that provides a simple to use, static interface such so you can do:

// Increase bond between me and the other by "2" friendliness for this event.
Friends.IncreaseBond(gameObject, collision.gameObject, 2); 

// Later on, you may want to see all the bonds you have accumumated:
var bonds = Friends.GetBonds(gameObject);
foreach (var bond in bonds)
    Debug.LogFormat("My bond with {0} is {1}.",, bond.value);

// Or just get a specific bond:
var bond = Friends.GetBond(gameObject, someOtherGameObject);

Not sure if i’ve got your problem but:
Create a Hashtable (or Dictionary) where you save pairs

Dictionary<GameObject, float> friendship = new Dictionary<GameObject, float>();

so, you can check if one gameobject has a friendship-level to another:

friendship.ContainsKey(gameobjectA); // return boolean

you can set a value like that:

friendship[gameobjectA] = 5.1f;

and with: friendship.Keys; you’ll get a list of all gameobjects in this dictionary, and with friendshipt.Values you’ll get all the values.