Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Raybrook · Nov 10, 2015 at 04:49 PM · objectvariablevalueforeachgenerate

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 :D

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Statement · Nov 10, 2015 at 09:38 PM

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;
     }
     else
     {
         // 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.other.name, bond.value);
 
 // Or just get a specific bond:
 var bond = Friends.GetBond(gameObject, someOtherGameObject);
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Soraphis · Nov 11, 2015 at 01:36 AM 0
Share

For the second solution, one should consider that you need some kind of order/sorting for the Friend class. So that Friends.IncreaseBond(gameObject, collision.gameObject, 2) is the same as Friends.IncreaseBond(collision.gameObject, gameObject, 2)

To archive that, i would implement an dictioniary . where friendship is an object taking two gameobjects, overloading the equal operator and hashvalue.

but it might go easier.

avatar image Raybrook · Nov 11, 2015 at 12:56 PM 0
Share

Hey, thanks all for your answers. It worked, but i not understand how to use the second part of this (Im using C#, maybe it's for it)... I must do it on another new script or in the same?

I have tried to do it on another script, but i cannot get the collider.gameObject who collides on the other script. And .IncreaseBond or .GetBond gives me an error.

Sorry if this questions seems like a bit stupid, im not an expert.

And thanks another time for all.

avatar image Statement Raybrook · Nov 11, 2015 at 02:34 PM 0
Share

The example provided is C#.

The second part, I guess you mean the Friends class idea?

You must write the class yourself. This is what I meant by "Consider implementing a class". It is up to you to create the class Friends with the methods IncreaseBond, GetBonds and GetBond if you want to have that functionality. This is only a suggestion on what you can do, not the way you must do it.

To get started, create a new .cs file called "Friends", and start typing.

I don't mean to impose onto you any design of how to implement that, but a skeleton could look like this:

 using UnityEngine;
 using System;
 using System.Collections.Generic;
 
 public static class Friends
 {
     // TODO: define data structures to store objects
 
     public static void IncreaseBond(GameObject left, GameObject right, float bond)
     {
         // TODO: Write this method.
         throw new NotImplementedException();
     }
 
     public static IEnumerable<Bond> GetBonds(GameObject obj)
     {
         // TODO: Write this function.
         throw new NotImplementedException();
     }
 
     public static Bond GetBond(GameObject obj)
     {
         // TODO: Write this function.
         throw new NotImplementedException();
     }
 }
 
 public sealed class Bond
 {
     // TODO: Write data required to implement your needs.
     //
     // For example, if you can alter the value of bond,
     // you may need to write back into Friends depending
     // on how you decide to implement your data structures.
 
     public GameObject other
     {
         get
         {
             // TODO: Write this propery.
             throw new NotImplementedException();
         }
     }
 
     public float value
     {
         get
         {
             // TODO: Write this propery. 
             //       $$anonymous$$aybe also add a setter.
             throw new NotImplementedException();
         }
     }
 }
avatar image Raybrook Statement · Nov 11, 2015 at 06:30 PM 0
Share

I think I understand it finally, thanks you again and all who have answered. I tried it and it worked, because for now i dont see any problem.

Again, thanks all :D

avatar image Statement Raybrook · Nov 11, 2015 at 06:43 PM 1
Share

Complete example project.

Unitypackage.

Or you can just glance the assets or code.

Example usage
Bond
BondAttribute
Dictionary2D

And more code that I didn't feel was necessary to get the picture. Oh, right. Picture:

avatar image
0

Answer by Soraphis · Nov 10, 2015 at 05:13 PM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Raybrook · Nov 10, 2015 at 07:39 PM 0
Share

Thanks for the answer. Im not and advanced user, and never used Dictionary, I suppose that it's like a List but it takes two variables. I dont know if you understand my question, as I have explained it badly (and im not english and doesnt speak so well :S). I hope with that picture i can explain my problem well:

![alt text][1] [1]: /storage/temp/57925-friendship.png

When the player collides with another gameobject, it's added to a list. What I mean is that when this object has been added to the list, generate a float variable,"friendship" ,
that is shared by these two objects , the player and the object that collided.

Thanks again for the answer, i hope solve this.

friendship.png (13.1 kB)
avatar image Statement Raybrook · Nov 10, 2015 at 09:19 PM 1
Share

Yes, that can be done with a dictionary. A dictionary is like a C++ std::map. I don't know if that help you at all.

But basically, a dictionary lets you look up something based on a "key".

Think of the analogy of a dictionary book. You want to know what "knowledge" is, so you go to the entry of "knowledge" (this is the key) and find "acquaintance with facts, truths, or principles, as from study or investigation; general erudition" (this is the value, mapped to the key).

But ins$$anonymous$$d of looking up "meanings of words", you are looking up "friendships of objects". The relationship in Soraphis example is implemented as a real number, where higher numbers mean tighter friendship.

So like with the dictionary example, you look up "what is my relationship with the green object?", if it doesn't exist, create a relationship (add the key and default value). Soraphis example suggests that the relationship to each object can be different (different numbers).

If you don't need different relations to different objects, a HashSet or List could do as well. Ins$$anonymous$$d of storing a unique value for each relationship, you could just settle on "I know who that is", and nothing more. Then you could just add the other object into a collection such as HashSet or List and see if it contains there on contact. If not, add it there so it knows for next time.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

37 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a float variable change when another variable changes? 1 Answer

Can you get a value from a object when destroyed? 2 Answers

Increase value by one each second [C#] 3 Answers

[Help] Getting variable based on string value. 2 Answers

Change a variable in a different object? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges