Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
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.

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

Best practice for object communication 3 Answers

creating a linked object mesh 0 Answers

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

Find Script from another script 2 Answers

how to link 1 var between 2 scripts ? 2 Answers


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