• 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 /
avatar image
0
Question by adelphiaUK · Dec 14, 2014 at 05:12 PM · objectscommunicationlinking

Linking object functions

I am creating a game which requires one object to perform a specific action when another (random) object performs the same action. Is it possible (using C#) to create two way communication between objects using handlers or the like?

In other words, when I rotate Object A by 90 degrees, Object B also rotates 90 degrees but in another scenario, it may be that when Object A is rotated 90 degrees, it's Object C that's required to rotate 90 degrees, and not Object B.

I hope I've made clear what I'm trying to accomplish.

Comment
Add comment · Show 1
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 adelphiaUK · Dec 14, 2014 at 05:34 PM 0
Share

O$$anonymous$$, I think I've sorted it, unless anyone has a better solution. What I have done is to create a list on the source object which the target object adds itself to if a link is required. Then, when the source object carries out the specific action, it iterates through the list and sends a function call back to all target objects.

1 Reply

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

Answer by SkaredCreations · Dec 14, 2014 at 05:23 PM

You could for example store the list of the objects in a script and when you need to get another random object use a function from there.

For example:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ObjectsList : MonoBehaviour {
 
     public static ObjectsList instance;

 // Drag all the available objects here from hierarchy panel
 // or else use FindObjectsOfType<YourScriptName>() in Awake or Start
     public GameObject[] allObjects;
 
     void Awake () {
         instance = this;
     }
     
     public GameObject GetRandomObject (GameObject excludeMe) {
         List<GameObject> list = new List<GameObject>(allObjects);
         list.Remove(excludeMe);
         return list[Random.Range(0, list.Count)];
     }
 }
 

Then in your script attached to the object you'll call the following line and apply your rotation or everything else to it:

 GameObject randomObject = ObjectsList.instance.GetRandomObject(gameObject);
Comment
Add comment · Show 1 · 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 adelphiaUK · Dec 14, 2014 at 10:22 PM 0
Share

Yeah, that's what I have ended up doing (as per my comment in my original post); for now anyway. I just don't like "answering" my own questions, so you get the credit ;)

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

27 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

Related Questions

Find Script from another script 2 Answers

Best practice for object communication 3 Answers

Two-way communication between parent and children-objects 1 Answer

creating a linked object mesh 0 Answers

if 20 objects are destroyed... 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges