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 RickyX · Jan 11, 2014 at 01:20 AM · c#read

Read from other script C#

First of all, don't think i'm a noob, becouse i'm not, i have a lot of experience with unity, but i never came into situation where i need to connect two scripts. So... I have my character, and let's say 5 triggers. Now every trigger has the same script, but thair vartables are not the same, i'll try to explain better:

Trigger 1's script looks like this: public Transform someTransform; // and i have set the someTransform to be BOX Trigger 2's script looks like this: public Transform someTransform; // AND I HAVE SET THIS someTransform TO BE SPHERE Trigger 3's script looks like this: public Transform someTransform; // I HAVE SET THIS someTransform TO BE CIRCLE ... More diffrent triggers

You see ? Every trigger has the same script, but scripts vars are not the same, first triggers script has var someTransform and someTransform is set as BOX, while 2nd's script is set to SPHERE, and 3rd's one is set to be CIRCLE

(Btw if you don't understand why BOX,SPHERE,CIRCLE. Well i just made an examples, thoose three are game objects ofcourse (transforms))

And so, when i step on the trigger, i want my character's script's var to = to the var of the trigger i stepped on...

God, if anyone made it at helping me i'm good, sorry guys i can't explain it better, i don't know how, if you don't understand something from here ask me, but please help me i need this... THANKS IN ADVANCE !

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

1 Reply

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

Answer by robertbu · Jan 11, 2014 at 01:31 AM

Your question is a bit confusing, but I'll take a shot. I'm assuming you are trying to get access to the 'someTransform' variable. Since you didn't give me a name, I'm using 'TriggerScript' for the name/class of the script that has 'someTransform'.

 void OnTriggerEnter(Collider collider) {
     TriggerScript ts = collider.GetComponent<TriggerScript>();
     if (ts != null) {
         Transform other = ts.someTransform;
     }
 }

This goes on the player.

Comment
Add comment · Show 4 · 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 sparkzbarca · Jan 11, 2014 at 01:40 AM 0
Share

just to note i dont think you can do collider.GetComponent directly

i think you have to do

collider.gameobject.getcomponent<...>();

avatar image robertbu · Jan 11, 2014 at 01:54 AM 1
Share

@sparkzbarca - Nope. Take a look at the script reference for the Collider Class. It inherits GetComponent().

avatar image RickyX · Jan 11, 2014 at 02:28 AM 0
Share

I think this is gonna do the job, i'll let you know when i test it out.

avatar image RickyX · Jan 11, 2014 at 02:53 AM 0
Share

Yeah, thank you so much, i just needed the idea how to get the var from the trigger i'm touching and change my var to that var from that trigger... Well i did some change on your code, now it looks like this:

void OnTriggerEnter(Collider other) { OverFenceTriggerScript ts = other.GetComponent(); obstacle = ts.Fence;

I didn't need Transform obstacle, it didn't work with that, just deleted Transform, changed the names of everything(changed collider to "other" too) and it worked

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

19 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# GetComponent Issue 2 Answers

Why don't my send messages work? 2 Answers

Grabing and moving a box(C#) 3 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