• 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 friends$$anonymous$$p 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 t$$anonymous$$s list. The problem is that I find no way to generate t$$anonymous$$s variable.

I hope to solve t$$anonymous$$s, 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 relations$$anonymous$$p 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 t$$anonymous$$s dude. He's my friend. Let's be more friends.
         friends[collision.gameObject] += 1f;
     }
     else
     {
         // A new dude. Dunno if I like $$anonymous$$m. Ok, yes I do.
         friends[collision.gameObject] = 1f;
     }
 }

However t$$anonymous$$s 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 t$$anonymous$$nks green is 10 friendly w$$anonymous$$le green t$$anonymous$$nks yellow is 3 friendly. If you want them both to share the same weight, you should let another class deal with the relations$$anonymous$$p. 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 t$$anonymous$$s 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
avatar image Raybrook · Nov 11, 2015 at 12:56 PM 0
Share
avatar image Statement Raybrook · Nov 11, 2015 at 02:34 PM 0
Share
avatar image Raybrook Statement · Nov 11, 2015 at 06:30 PM 0
Share
avatar image Statement Raybrook · Nov 11, 2015 at 06:43 PM 1
Share
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> friends$$anonymous$$p = new Dictionary<GameObject, float>();

so, you can check if one gameobject has a friends$$anonymous$$p-level to another:

 friends$$anonymous$$p.ContainsKey(gameobjectA); // return boolean

you can set a value like that:

 friends$$anonymous$$p[gameobjectA] = 5.1f;

and with: friends$$anonymous$$p.Keys; you'll get a list of all gameobjects in t$$anonymous$$s dictionary, and with friends$$anonymous$$pt.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
friendship.png (13.1 kB)
avatar image Statement Raybrook · Nov 10, 2015 at 09:19 PM 1
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

36 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

Related Questions

Passing in objects to class constructor 0 Answers

Changing a variable value 2 Answers

Change positive to negative negative to positive 2 Answers

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

Change a variable value for 5 seconds 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