# Error using GetComponent\<\>() to get a script -- type or namespace can't be found

**URL:** https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412
**Category:** Scripting
**Tags:** Scripting
**Created:** [November 27, 2012, 4:22pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412 "2012-11-27T16:22:39Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![Yifan](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yifan](https://discussions.unity.com/u/Yifan)
#### Post date: [November 27, 2012, 4:22pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/1 "2012-11-27T16:22:39Z")

</div>

I am trying to disable a script attached to my first person controller camera. Here is my simple code.

```auto
void OnGUI(){
		if(GUI.Toggle(new Rect (100,100,100,100), false, "camera" )){
			GetComponent<Test>().enabled = false;	
		}
	}

```

For the default scripts that comes with the first person controller, such as “Mouse Look”, I can put their script names in \<\> to get them. However, when I put my script within the \<\>, I get an error saying “The type or namespace ‘Test’ could not be found (are you missing a using directive or an assembly reference?)”

The type of a script should be the script name, why it says the type can not be found? I tried to declare a variable that is of the type of the script, and get the same error. How can I let the compiler know I am referring to a script created by me?

Thank you!

---

<div class="post-metadata">

### Author: ![lordofduct](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lordofduct/32/890751_2.png) [@lordofduct](https://discussions.unity.com/u/lordofduct)
#### Post date: [November 27, 2012, 4:26pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/2 "2012-11-27T16:26:05Z")

</div>

are you sure it’s created correctly, that the name matches up, and that it’s not in some namespace (all components/monobehaviours that can be added to gameobjects can’t be in a namespace).

Also is this ‘Test’ script possibly written in unityscript. I’ve heard noise about compiling order between C# and javascript, and C# can’t recognize the unityscript script files because they get compiled after the C# or something. (I don’t know for certain if this is true, I use all C#)

---

<div class="post-metadata">

### Author: ![willemsenzo](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/willemsenzo/32/537442_2.png) [@willemsenzo](https://discussions.unity.com/u/willemsenzo)
#### Post date: [November 27, 2012, 4:29pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/3 "2012-11-27T16:29:15Z")

</div>

I believe you have to be explicit and write something like: gameObject.GetComponent

---

<div class="post-metadata">

### Author: ![Landern](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/landern/32/170836_2.png) [@Landern](https://discussions.unity.com/u/Landern)
#### Post date: [November 27, 2012, 4:44pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/4 "2012-11-27T16:44:23Z")

</div>

> [@willemsenzo](#):
>
> I believe you have to be explicit and write something like: gameObject.GetComponent

That isn’t accurate. The call GetComponent without any object specified and if the class derives from Monobehavior will be executed on the object instance that the OnGUI script example(from Yifan) is attached to. I suppose

```auto
this.GetComponent<SomeScriptType>();

```

might look clearer?

---

<div class="post-metadata">

### Author: ![willemsenzo](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/willemsenzo/32/537442_2.png) [@willemsenzo](https://discussions.unity.com/u/willemsenzo)
#### Post date: [November 27, 2012, 4:52pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/5 "2012-11-27T16:52:11Z")

</div>

Well isn’t the result the same in this case? .this refers to the game object this script is attached to, gameObject also refers to the game object the script is attached to. If I’m wrong let me know I’m still learning myself 🙂

By the way I was aware I didn’t specify the component but that wasn’t the point I was trying to make.

---

<div class="post-metadata">

### Author: ![Yifan](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yifan](https://discussions.unity.com/u/Yifan)
#### Post date: [November 27, 2012, 4:55pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/6 "2012-11-27T16:55:18Z")

</div>

Thank you Lordofduct!

In C# script, it does have problem with recognizing unityscripts.

Just curious have you ever met situation that you really don’t know what’s wrong with your scripts, but when you rewrite a new one maybe after you restart your unity, everything works again, although you wrote the same code as last time?

---

<div class="post-metadata">

### Author: ![Eric5h5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/eric5h5/32/44225_2.png) [@Eric5h5](https://discussions.unity.com/u/Eric5h5)
#### Post date: [November 27, 2012, 4:57pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/7 "2012-11-27T16:57:33Z")

</div>

> [@willemsenzo](#):
>
> I believe you have to be explicit and write something like: gameObject.GetComponent

Nope. You don’t need “this” or “gameObject” or anything, just GetComponent by itself is fine.

> [@lordofduct](#):
>
> Also is this ‘Test’ script possibly written in unityscript. I’ve heard noise about compiling order between C# and javascript, and C# can’t recognize the unityscript script files because they get compiled after the C# or something. (I don’t know for certain if this is true, I use all C#)

[http://docs.unity3d.com/Documentation/ScriptReference/index.Script\_compilation\_28Advanced29.html](http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html) (particularly section 3)

–Eric

---

<div class="post-metadata">

### Author: ![Yifan](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yifan](https://discussions.unity.com/u/Yifan)
#### Post date: [November 27, 2012, 4:58pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/8 "2012-11-27T16:58:54Z")

</div>

Hi Will,

Actually you can skip the gameObject or this before GetComponent when you are getting components from the same object that this script is attached to.

It may be a shortcut for unity.

---

<div class="post-metadata">

### Author: ![willemsenzo](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/willemsenzo/32/537442_2.png) [@willemsenzo](https://discussions.unity.com/u/willemsenzo)
#### Post date: [November 27, 2012, 5:01pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/9 "2012-11-27T17:01:04Z")

</div>

Thats good to know I learned something 🙂

---

<div class="post-metadata">

### Author: ![willemsenzo](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/willemsenzo/32/537442_2.png) [@willemsenzo](https://discussions.unity.com/u/willemsenzo)
#### Post date: [November 27, 2012, 5:04pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/10 "2012-11-27T17:04:50Z")

</div>

I understand thanks for clearing that up 🙂

---

<div class="post-metadata">

### Author: ![Yifan](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yifan](https://discussions.unity.com/u/Yifan)
#### Post date: [November 27, 2012, 5:24pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/11 "2012-11-27T17:24:16Z")

</div>

> [@Eric5h5](#):
>
> Nope. You don’t need “this” or “gameObject” or anything, just GetComponent by itself is fine.
> 
> [http://docs.unity3d.com/Documentation/ScriptReference/index.Script\_compilation\_28Advanced29.html](http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html) (particularly section 3)
> 
> –Eric

Can you explain a little bit in the section 3, what does it mean by the “editor”? Is it a folder? I am confused. The “editor” to me meant the unity software interface that we’ve been working on.

BTW, thank you for answering my questions yesterday!

---

<div class="post-metadata">

### Author: ![Landern](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/landern/32/170836_2.png) [@Landern](https://discussions.unity.com/u/Landern)
#### Post date: [November 27, 2012, 5:46pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/12 "2012-11-27T17:46:54Z")

</div>

It it referring to folders in your project hierarchy.

---

<div class="post-metadata">

### Author: ![BPPHarv](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bppharv/32/363743_2.png) [@BPPHarv](https://discussions.unity.com/u/BPPHarv)
#### Post date: [November 27, 2012, 5:50pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/13 "2012-11-27T17:50:36Z")

</div>

In section 3 “Editor” refers to a folder/directory in your project. This folder contains specialized scripts that use or modify the actual editing process which are not appropriate or compatible with the game build. So scripts that add functionality to Unity’s editor menus and such should be put in this “Editor” folder.

---

<div class="post-metadata">

### Author: ![Yifan](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yifan](https://discussions.unity.com/u/Yifan)
#### Post date: [November 27, 2012, 5:55pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/14 "2012-11-27T17:55:33Z")

</div>

> [@](#):
>
> It it referring to folders in your project hierarchy

> [@BPPHarv](#):
>
> In section 3 “Editor” refers to a folder/directory in your project. This folder contains specialized scripts that use or modify the actual editing process which are not appropriate or compatible with the game build. So scripts that add functionality to Unity’s editor menus and such should be put in this “Editor” folder.

That is not a default folder, right? So when you need to add functionality to Unity’s editor menu, you create a folder called “Editor” and put your related scripts in it?

---

<div class="post-metadata">

### Author: ![BPPHarv](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bppharv/32/363743_2.png) [@BPPHarv](https://discussions.unity.com/u/BPPHarv)
#### Post date: [November 27, 2012, 9:55pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/15 "2012-11-27T21:55:22Z")

</div>

> [@Yifan](#):
>
> That is not a default folder, right? So when you need to add functionality to Unity’s editor menu, you create a folder called “Editor” and put your related scripts in it?

I believe this is the case. If the folder doesn’t exist you create it. I put mine as a sub directory of assets and most of the info I’ve seen seems to use this as a standard but I did see a post where it was mentioned “so long as it’s in the assets folder hierarchy” but have no experience with this case.

_"The AssetDatabase interface is only available in the editor and has no function in the built player. Like all other editor classes, it is only available to scripts placed in the Editor folder (just create a folder named �Editor� in the main Assets folder of your project if there isn’t one already). "_

[http://docs.unity3d.com/Documentation/Manual/AssetDatabase.html](http://docs.unity3d.com/Documentation/Manual/AssetDatabase.html)

---

<div class="post-metadata">

### Author: ![Yifan](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yifan](https://discussions.unity.com/u/Yifan)
#### Post date: [November 27, 2012, 10:24pm UTC](https://discussions.unity.com/t/error-using-getcomponent-to-get-a-script-type-or-namespace-cant-be-found/491412/16 "2012-11-27T22:24:21Z")

</div>

> [@BPPHarv](#):
>
> I believe this is the case. If the folder doesn’t exist you create it. I put mine as a sub directory of assets and most of the info I’ve seen seems to use this as a standard but I did see a post where it was mentioned “so long as it’s in the assets folder hierarchy” but have no experience with this case.
> 
> _"The AssetDatabase interface is only available in the editor and has no function in the built player. Like all other editor classes, it is only available to scripts placed in the Editor folder (just create a folder named �Editor� in the main Assets folder of your project if there isn’t one already). "_
> 
> [http://docs.unity3d.com/Documentation/Manual/AssetDatabase.html](http://docs.unity3d.com/Documentation/Manual/AssetDatabase.html)

I got it. Thank you and many other folks!
