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
-1
Question by JonnyM · Apr 05, 2014 at 06:43 PM · c#component

How can I access values from a component script in another object

I have a very simple scene with a single cube, a camera and a light. I've written a C# class which processes user input swipe gestures from either touches or from the mouse, and also for specific regions of the screen.

I've attached the script twice as components of the camera. One to service swipes on the left hand side and one to service swipes on the right (there is a long drawn out reason for this that I won't go into here). All works fine and the scripts function as expected.

I'd now like to move the cube in the direction of the swipe, but can't figure out how I can get the values from the swipe controllers (attached to the camera) to the code which animates the cube.

In short, (as titled) - how can I access values from my swipe controller scripts in my cube controller script. Also, if you have the same script attached to the same object more than once, how does Unity differentiate between the two?

Hope that makes sense, Many thanks,

Comment
Add comment · Show 4
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 DaveA · Apr 05, 2014 at 06:44 PM 3
Share

I'm surprised you didn't either google this or use the Search field, above. This gets asked several times per day, every day, for the last 5 years.

avatar image JonnyM · Apr 05, 2014 at 06:49 PM 0
Share

I'm more surprised that you actually think I didn't. If I'd found anything that worked, I wouldn't be posting here. This page didn't seem to help much. I know how to access a script of a specific type, but not when it's set as a component of another object.

avatar image getyour411 · Apr 05, 2014 at 06:54 PM 1
Share

http://answers.unity3d.com/questions/11601/can-i-have-multiple-components-of-the-same-type-on.html

There are a thousand examples and you gave no indication you looked or tried any.

avatar image JonnyM · Apr 05, 2014 at 07:08 PM 0
Share

Yes, but part of the problem is that if you don't know what you're looking for - it's hard to find the right question. At this stage, I don't even know if the question I posted is the right ter$$anonymous$$ology. Only started unity this morning.

1 Reply

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

Answer by origonn · Apr 05, 2014 at 06:54 PM

To find the scripts from a different object, just do

 GameObject.Find("GameObjectName").GetComponents<Script>();

This will return an array of Script types, at which point its up to you to figure out which one the [0] and [1] elements refer to. Unless you change the order in the editor however, the order should always be the same

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 JonnyM · Apr 05, 2014 at 07:15 PM 0
Share

Finally, this put me on the right track (in combination with 'Camera.main'). Thank you!

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Problems with Audio Sources 1 Answer

Distribute terrain in zones 3 Answers

How to disable script in other object 1 Answer

Show the GUI button when mouse hover to the 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