- Home /
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 !
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.
just to note i dont think you can do collider.GetComponent directly
i think you have to do
collider.gameobject.getcomponent<...>();
@sparkzbarca - Nope. Take a look at the script reference for the Collider Class. It inherits GetComponent().
I think this is gonna do the job, i'll let you know when i test it out.
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
Follow this Question
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