• 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
1
Question by Sam 5 · Mar 03, 2011 at 03:39 AM · multiple

Is it possible to get two scripts from different objects to talk to each other without assigning objects in the inspector?

Ok, for reference I'm doing a spaceship game in Boo. I have a ship, it is parent to a camera and an empty that acts as an anchor point for a weapon of the player's choice. Each of these potential weapons has a script that needs a variable from a script running on the camera.

Now in my 1st prototype the anchor point empty was actually the weapon itself and was fixed, unchanging. As such I could use:

public Targetscript as Targeting1
public Gpos as Vector3
def update():
    Gpos = Targetscript.GunneryPOS

where Targeting1 is the name of the script attached to the camera, and GunneryPOS is the variable I need to pull. Drag and drop the camera to Targeting1 in the inspector and everything was happy. Now however, with swapping out weapons and possibly having multiple ships I feel like the drag and drop to the inspector is not particularly flexible.

I feel like the answer I'm looking for has something to do with transform.root and/or transform.Find(). I want to say "Hey weapon, you are now part of a ship. Go look for the camera that's also part of the ship and pull GunneryPOS from Targeting1." I'm just not sure quite how.

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 Peter G · Mar 03, 2011 at 03:45 AM 0
Share

Wow, someone who writes in Boo, +1 for that.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by by0log1c · Mar 03, 2011 at 04:16 AM

I'm more familiar with JS but I've used Unity scripting reference to write it in BOO(quite similar after,all?) after reading Peter's comment, here's an example:

//for a specific instance on a specific GameObject: Targetscript = GameObject.Find('theGameObjectName').GetComponent[of Targeting1]();

//or : Targetscript = FindObjectOfType(Targeting1); Targetscripts = FindObjectsOfType(Targeting1); //also works

AFAIK, .Find and .GetComponent are considered slow only if used repeatedly. The idea is to store the result in a variable like the example above.

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Help using multiple triggers with multiple scripts. 1 Answer

How can I use a master script to send values to multiples objects 1 Answer

Multiple scripts with the same name issue 1 Answer

How do I get each GameObject to mind its own business? 1 Answer

Accessing an Array in another function 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