# Accessing scripts between GameObjects

**URL:** https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116
**Category:** Scripting
**Tags:** Scripting
**Created:** [May 16, 2011, 9:53am UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116 "2011-05-16T09:53:19Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![gharaibeh](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/gharaibeh/32/279043_2.png) [@gharaibeh](https://discussions.unity.com/u/gharaibeh)
#### Post date: [May 16, 2011, 9:53am UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/1 "2011-05-16T09:53:19Z")

</div>

**I have two GameObjects, each of them have their own scripts in C# , my Question is how to access the variables from one scripts to another , or how to send parameters between two objects ???**

😵‍💫😵‍💫😵‍💫😵‍💫

---

<div class="post-metadata">

### Author: ![Afisicos](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/afisicos/32/131583_2.png) [@Afisicos](https://discussions.unity.com/u/Afisicos)
#### Post date: [May 16, 2011, 9:58am UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/2 "2011-05-16T09:58:49Z")

</div>

var script1 :“NAME OF THE SCRIPT WITHOUT SPACES”; //later u have to atach in the inspector the GameObject that have the script.

script1.“NAME OF VARIABLE”=XXXX;

---

<div class="post-metadata">

### Author: ![Quietus2](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/quietus2/32/444325_2.png) [@Quietus2](https://discussions.unity.com/u/Quietus2)
#### Post date: [May 16, 2011, 10:18am UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/3 "2011-05-16T10:18:34Z")

</div>

[http://unity3d.com/support/documentation/ScriptReference/index.Accessing\_Other\_Game\_Objects.html](http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html)

^ You should download and reinstall Unity if your copy did not come with documentation.

---

<div class="post-metadata">

### Author: ![anon\_58446700](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@anon\_58446700](https://discussions.unity.com/u/anon_58446700)
#### Post date: [May 16, 2011, 10:58am UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/4 "2011-05-16T10:58:43Z")

</div>

> [@Quietus2](#):
>
> [http://unity3d.com/support/documentation/ScriptReference/index.Accessing\_Other\_Game\_Objects.html](http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html)
> 
> ^ You should download and reinstall Unity if your copy did not come with documentation.

Is that your idea of being helpful? Really? Do you know of any version of Unity that comes without the offline docs? Do you know of anyone that can find and signup for this forum and have posted 13 messages on it without having accidentally stumbled upon the online docs?

Really? Or are you such a pious twat you figured making a snarky attempt at “RTFM” was an answer?

@gharaibeh sorry mate. I can’t help you directly… I struggle with this same issue in UnityScript everytime I need it. Best bet is UnityAnswers, search for [accessing] [variables] [script] [C#] or something like that.

Object, component and script communication is IMHO the biggest single hurdle in coming to terms with Unity. Because it’s so fucking badly described, featured and explained. If they put 1/100th of the effort they put into marketing into explaining this feature, they’d have more users with more joy.

It is Unity’s greatest strength, this approach to objects and their attached scripts and the fixedupdate, lateupdate etc features that allow you to treat them all independently. It’s a beautiful construct to experiment and play. Truly… But…

Why they can’t document and explain this absolute fundamental and most marketable of their features is completely fucking beyond me. They (staff and programmers of Unity) don’t need it explained, so likely figured no one else did either. Or some other tomfoolery, mickey-mouse mental approach to the inherent feature of Unity.

It’s a bit like Flash not doing a great job of explaining the timeline. Or Photoshop not explaining layers well. Or 3ds max not taking the time to point out the benefits and beauties and inherent relationship between itself and its stack. Or ice cream without cream.

I’ve harped on and on and on about this many a time… but nobody seems to see it for what it is… the best feature of the program hidden behind an accidental veil of secrecy and misdirection. Further confounded by the all to common approach of the @Quietus above.

DO NOT BE PUT OFF if the link he’s given you doesn’t make sense the first dozen times you read it. Simply go find all the examples you can find and all other REAL answers to your question you can.

And if you feel the urge, after having found out exactly how it works, please share, via a meaningful tutorial with diagrams and arrows and highlighting and syntax explanations. The world needs it. Well, the Unity world of users yet to use it does. And one day, when I bother learning C#, I’ll be reading it.

The programmers and forum hounds for whom it’s clear and innate how this all works within Unity, you’re not obliged to comment, unless you can help our dear friend gharaibeh and any others that come this way based on the title of his thread. Not help in the sense that you feel better about it, help in the sense that it might actually help gharaibeh.

Don’t try to rationalise it with "you should go learn OOP somewhere else… " etc. that paths been covered. It’s a path away from Unity. He’ll never come back. And believe me, you lovers of Unity, you want him to stay here, to buy every license he can afford, to have great success with Unity and then prosper, to then buy seats and seats and seats of licenses for his next projects with Unity… and keep your darling ship afloat.

Anyone from Unity reading this? See a pattern here, other than me berating your docs and marketing? That this question about object/script/component/variable communication comes up time and time and time again??? do you see this? Do you ever wonder why? beyond assuming that they haven’t RTFM? Or that they’re inherently inferior to your great understanding of all things… If your answer is, in anyway, a “hummmm…” or “maybe” or “yes”, then it’s on you to come up with a solution.

If, as I suspect, you never care what the punters do, then carry on. The studios might wanna buy another couple licenses, or want you to make a game for them on the basis of a raft of promise, or a kiss.

---

<div class="post-metadata">

### Author: ![Marrrk](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/marrrk/32/313405_2.png) [@Marrrk](https://discussions.unity.com/u/Marrrk)
#### Post date: [May 16, 2011, 11:58am UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/5 "2011-05-16T11:58:15Z")

</div>

What a rant ^^

Script1 attached to Object A (Name: “A” ) wants to change the public member variable Foo of Script2 in Object B (Name: “B”) to the value “Hello World”:

```auto
var b = GameObject.Find("B");
var two = b.GetComponent<Script2>();
two.Foo = "Hello World";

```

I dont know what you expect dissidently, but the code looks easy to me.  
Maybe you want something like this:

```auto
ChangeValue(@"B\Script1\Foo", "Hello World");

```

Easy:

```auto
public void ChangeValue(string path, object value)
{
  var pathParts = path.Split(new char[] {'\\', '/'});
  var gameObject = GameObject.Find(pathParts[0]);
  var scriptComponent = gameObject.GetComponent(pathParts[1]);
  var scriptComponent.GetField(pathParts[0]).SetValue(scriptComponent, value);
}

```

But why should someone use this if its already so easy.

---

<div class="post-metadata">

### Author: ![gharaibeh](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/gharaibeh/32/279043_2.png) [@gharaibeh](https://discussions.unity.com/u/gharaibeh)
#### Post date: [May 17, 2011, 11:51am UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/6 "2011-05-17T11:51:07Z")

</div>

**I solved it , thanks Marrrk**

---

<div class="post-metadata">

### Author: ![anon\_7954552](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@anon\_7954552](https://discussions.unity.com/u/anon_7954552)
#### Post date: [May 19, 2011, 7:18am UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/7 "2011-05-19T07:18:54Z")

</div>

@dissidently … I agree.

I read the documentation, but it left me with lots of questions. Why should I be using a name to access it. I have the same script on lots of gameObjects… but I only want to change certain game objects.

I would expect something like gameObject.GetScript(“whatever”).myvalue = 1;

I read Marrrk comment and he doesn’t seem to do it any better.

why GameObject.Find(“B”)… is that really necessary…why not gameObject.GetComponent why all the unnecessary information??

hey I’m new Unity3D and documentation like this is kinda useless:

“To access it from another script you need to use the name of the script followed by a dot and the global variable name.print(TheScriptName.someGlobal);  
TheScriptName.someGlobal = 10;”

Peter

---

<div class="post-metadata">

### Author: ![Marrrk](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/marrrk/32/313405_2.png) [@Marrrk](https://discussions.unity.com/u/Marrrk)
#### Post date: [May 19, 2011, 7:44am UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/8 "2011-05-19T07:44:16Z")

</div>

> [@](#):
>
> I read Marrrk comment and he doesn’t seem to do it any better.

Thats because there is nothing to do better.

> [@](#):
>
> I would expect something like gameObject.GetScript(“whatever”).myvalue = 1;

If you have your gameObject with the script you can do that, except that its called GetComponent and not GetScript. Tell us where you fail at and we will help you.

> [@](#):
>
> why GameObject.Find(“B”)… is that really necessary…why not gameObject.GetComponent why all the unnecessary information??

What do you expect? You need to search the gameobject, if you didnt already found it, before you can search/access the script you want to change.

The documentation is far from beeing perfect, but I find the “Accessing Other GameObjects” article very good. If you are not able to grasp the concept behind all of this I suggest you learn the Unity basics or ask questions instead of flaming how bad everything is.

---

<div class="post-metadata">

### Author: ![npsf3000\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/npsf3000_1/32/466497_2.png) [@npsf3000\_1](https://discussions.unity.com/u/npsf3000_1)
#### Post date: [May 19, 2011, 8:00am UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/9 "2011-05-19T08:00:45Z")

</div>

> [@anon\_7954552](#):
>
> I would expect something like gameObject.GetScript(“whatever”).myvalue = 1;

That is exactly how it is.

```auto
//working JS
gameObject.GetComponent(Transform).position += Vector3.forward;
//simplified
transform.position += Vector3.forward;

```

Is a common example. (gameObject refers to the current game object a component is attached to and can usually be left out).

> [@anon\_7954552](#):
>
> why GameObject.Find(“B”)… is that really necessary…why not gameObject.GetComponent why all the unnecessary information??

How is it unnecessary?

GameObject.Find(“B”);

Does what it says it does in the docs:

_ **Finds a game object by name and returns it.** _

[http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html](http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html)

It is not the only way of doing this, but without knowing _which_ game object your talking about, how do can you change a property of it?

Do note: By default a script knows which object it is attached to, so to change your own properties it is much easier.

> [@anon\_7954552](#):
>
> hey I’m new Unity3D and documentation like this is kinda useless:
> 
> “To access it from another script you need to use the name of the script followed by a dot and the global variable name.print(TheScriptName.someGlobal);  
> TheScriptName.someGlobal = 10;”  
> Peter

That doc is quite accurate, and says basically what you asked for earlier:

```auto
gameObject.GetScript("whatever").myvalue

```

You need the script name:

```auto
GetScript("whatever") //though for proper function see GameObject.GetComponent

```

followed by a dot:

```auto
.

```

followed by a global variable:

```auto
myvalue

```

There is nothing wrong in learning, and it is sometimes difficult, but what you need to do is relax and ask questions, not immediately blame the docs. In this case how unity works, and how you expect it to work appear to be very similar, if not exactly the same. Have you tried using the many scripts in the various docs and tutorials that actually work?

---

<div class="post-metadata">

### Author: ![anon\_58446700](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@anon\_58446700](https://discussions.unity.com/u/anon_58446700)
#### Post date: [May 19, 2011, 8:05am UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/10 "2011-05-19T08:05:38Z")

</div>

> [@Marrrk](#):
>
> Thats because there is nothing to do better.

Are you serious? There’s nothing about it that can be done better? Your description and the online docs description of this process could not, in any way, be made better or improved in any way?

There is a universe in which you’re correct. Where you and everything you say cannot be improved upon, where there is only one point of reference. That’s your universe. The one where everybody thinks in exactly the same way you do. Because they are you. Unfortunately in that Universe you can’t help anyone, because everyone is the same. And nobody can help you. Because they’re all you. So you’re at once surrounded, and alone.

> [@](#):
>
> If you are not able to grasp the concept behind all of this I suggest you learn the Unity basics or ask questions instead of flaming how bad everything is.

This looks like derisive flaming to me. Everything involving thought is subjective Marrrk. EVERYTHING. And for some, this issue of communication between objects, components, scripts etc is not as it could be, or as we perceive it might be, it is, instead, obscured behind untold methodologies and some of the worst written documentation around ANY programs key features of all time.

Congrats to you Marrrk for figuring it out. Now take a deep breath, and look inside your other selves, see if you can find a less perfect version of yourself that might struggle with the docs and explanations as they are… and try to help that lesser being to see what you see.

---

<div class="post-metadata">

### Author: ![Marrrk](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/marrrk/32/313405_2.png) [@Marrrk](https://discussions.unity.com/u/Marrrk)
#### Post date: [May 19, 2011, 8:17am UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/11 "2011-05-19T08:17:25Z")

</div>

@dissidently: tl/dr

Switch off the herp derp mode and understand what the documentation tries to teach you, Its not that hard.

---

<div class="post-metadata">

### Author: ![anon\_58446700](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@anon\_58446700](https://discussions.unity.com/u/anon_58446700)
#### Post date: [May 19, 2011, 8:17am UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/12 "2011-05-19T08:17:44Z")

</div>

@ Crisium : I have it on good authority that the docs make a great reference once you’ve learnt how Unity weaves its communication magic. However you’ll need to learn how it works somewhere else.

This place, Unity and it’s forums, only has two kinds of people.

those that get it.

those that don’t get it.

Those that get it have no idea how to communicate with those that don’t get it in a way that helps them get it.

Those that don’t get it have no idea how to frame the question so they might have a chance of someone getting it, getting their question.

Good luck.

---

<div class="post-metadata">

### Author: ![anon\_58446700](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@anon\_58446700](https://discussions.unity.com/u/anon_58446700)
#### Post date: [May 19, 2011, 8:20am UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/13 "2011-05-19T08:20:22Z")

</div>

Marrrk the documentation makes no effort to teach. It’s a reference file. Not an educational file. I’m happy for you, that you get it all, and know it all. Congrats. Well done. Bravo. Hurrah.

The great big next step for you, and all similar, is in understanding that two people reading the same thing actually see two different things. If you can make that step, the world will open up in ways you can’t possibly currently imagine.

---

<div class="post-metadata">

### Author: ![KyleStaves\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kylestaves_1/32/419826_2.png) [@KyleStaves\_1](https://discussions.unity.com/u/KyleStaves_1)
#### Post date: [May 19, 2011, 6:28pm UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/14 "2011-05-19T18:28:03Z")

</div>

So what exactly do you want to learn dissidently? What do you think is missing from the tutorials and example projects ([Unity Learn](http://unity3d.com/support/resources/))?

From what I understand, the major issue is accessing components on other game objects, and variables on those components - correct?

If I wrote a tutorial that was broken down into something like:

- Why C# over UnityScript(JavaScript)

- Understanding Variables

- Private

- Public

- Protected/Internal

- Understanding GameObject and Component

- GameObjects

- Accessing GameObjects

- Components

- Accessing Components

- Example Project

- Setting Up Scene

- Basic Script (accessing other game objects and Unity components on those game objects)

- Advanced Script (creating a custom MonoBehavior, accessing said component on other game objects)

- Singleton’s in Unity

Do you feel this would cover what you think is missing?

---

<div class="post-metadata">

### Author: ![anon\_58446700](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@anon\_58446700](https://discussions.unity.com/u/anon_58446700)
#### Post date: [May 19, 2011, 6:54pm UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/15 "2011-05-19T18:54:35Z")

</div>

> [@KyleStaves\_1](#):
>
> So what exactly do you want to learn dissidently? What do you think is missing from the tutorials and example projects ([http://unity3d.com/support/resources/](http://unity3d.com/support/resources/))?
> 
> From what I understand, the major issue is accessing components on other game objects, and variables on those components - correct?
> 
> If I wrote a tutorial that was broken down into something like:
> 
> - Why C# over UnityScript(JavaScript)
> 
> - Understanding Variables
> 
> - Private
> 
> - Public
> 
> - Protected/Internal
> 
> - Understanding GameObject and Component
> 
> - GameObjects
> 
> - Accessing GameObjects
> 
> - Components
> 
> - Accessing Components
> 
> - Example Project
> 
> - Setting Up Scene
> 
> - Basic Script (accessing other game objects and Unity components on those game objects)
> 
> - Advanced Script (creating a custom MonoBehavior, accessing said component on other game objects)
> 
> - Singleton’s in Unity
> 
> Do you feel this would cover what you think is missing?

Sort of. I’d like to start it from a different point of view though. From a “this is the Unity methodology to time, space and intereaction” point of view. And explain how update functions are inherent to what you’ve listed, just as scripts and components being attached to objects are, and then, yes, largely dive into what you’re saying.

All done visually. ie. diagrams visually blocking out text, objects, components, scripts etc with clear highlighting of syntax, blocks of cause and effect outlined, flow arrowed and described etc. Kind of an educational flow chart of how Unity (for lack of a better word here) thinks.

Does this make sense?

If you’re serious about doing this, I’ll happily do all the graphics, layout, visuals etc. And we can PDF it nicely. I’ve been trying to find someone to do this with for a while. I consider myself a near perfect guinea pig, cause if it makes perfect sense to me, there won’t be too many that don’t get it. And have sort of held of learning any C# until being able to be that guinea pig… so I come at it completely fresh, naive and ignorant.

And a couple bottles of vodka should ensure I forget every piece of UnityScript I’ve learnt. So I can start again full proper ignorant.

---

<div class="post-metadata">

### Author: ![Quietus2](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/quietus2/32/444325_2.png) [@Quietus2](https://discussions.unity.com/u/Quietus2)
#### Post date: [May 19, 2011, 7:09pm UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/16 "2011-05-19T19:09:06Z")

</div>

It is not Unity’s job to teach you how to program. It is a game engine, not a “programming for dummies” classroom. There are many such tutorials available on the interwebs.

Unity may lower the bar of entry with it’s ease of rapid iteration, however you are still expected to come to the table with a certain set of skills before you sit down. The documentation is clear and concise for anyone who has a _modicum_ of application development experience. That’s all it needs to be. It serves it’s purpose.

The problem you are having is not an issue with the software, nor it’s documentation. It’s a hardware error, namely the faulty device between the table and the chair and it’s unreasonable expectations.

---

<div class="post-metadata">

### Author: ![anon\_58446700](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@anon\_58446700](https://discussions.unity.com/u/anon_58446700)
#### Post date: [May 19, 2011, 7:21pm UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/17 "2011-05-19T19:21:49Z")

</div>

> [@Quietus2](#):
>
> It is not Unity’s job to teach you how to program. It is a game engine, not a “programming for dummies” classroom. There are many such tutorials available on the interwebs.
> 
> Unity may lower the bar of entry with it’s ease of rapid iteration, however you are still expected to come to the table with a certain set of skills before you sit down. The documentation is clear and concise for anyone who has a _modicum_ of application development experience. That’s all it needs to be. It serves it’s purpose.
> 
> The problem you are having is not an issue with the software, nor it’s documentation. It’s a hardware error, namely the faulty device between the table and the chair and it’s unreasonable expectations.

yeah. thanks. great. the “go learn somewhere else” thing again… non original. non helpful. but so long as you feel better, then everything’s AllllLLLrrrrrrRRIGHTttt!

---

<div class="post-metadata">

### Author: ![Quietus2](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/quietus2/32/444325_2.png) [@Quietus2](https://discussions.unity.com/u/Quietus2)
#### Post date: [May 19, 2011, 7:26pm UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/18 "2011-05-19T19:26:12Z")

</div>

> [@anon\_58446700](#):
>
> yeah. thanks. great. the “go learn somewhere else” thing again… non original. non helpful. but so long as you feel better, then everything’s AllllLLLrrrrrrRRIGHTttt!

It’s reality.

“I haven’t a clue what I’m doing, it must be Unity’s fault” is quite a bizarre perspective. Maybe you could try sleeping at a Holiday Inn Express, I saw on TV that works for some people.

---

<div class="post-metadata">

### Author: ![anon\_58446700](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@anon\_58446700](https://discussions.unity.com/u/anon_58446700)
#### Post date: [May 19, 2011, 7:43pm UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/19 "2011-05-19T19:43:32Z")

</div>

> [@Quietus2](#):
>
> It’s reality.
> 
> “I haven’t a clue what I’m doing, it must be Unity’s fault” is quite a bizarre perspective. Maybe you could try sleeping at a Holiday Inn Express, I saw on TV that works for some people.

I think you’ve missed a few beats of the drum. Don’t worry. The band plays on.

---

<div class="post-metadata">

### Author: ![Quietus2](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/quietus2/32/444325_2.png) [@Quietus2](https://discussions.unity.com/u/Quietus2)
#### Post date: [May 19, 2011, 7:51pm UTC](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116/20 "2011-05-19T19:51:05Z")

</div>

> [@anon\_58446700](#):
>
> I think you’ve missed a few beats of the drum. Don’t worry. The band plays on.

I joined the band right after I purchased the trumpet without knowing a thing about music. It’s obviously the trumpet manufacturer’s fault, for they didn’t include detailed instructions on how to read sheet music and transpose scales.

[Next page](https://discussions.unity.com/t/accessing-scripts-between-gameobjects/445116.md?page=2)
