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

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

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!

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#)

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

1 Like

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

this.GetComponent<SomeScriptType>();

might look clearer?

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 :slight_smile:

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

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?

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 (particularly section 3)

–Eric

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.

Thats good to know I learned something :slight_smile:

I understand thanks for clearing that up :slight_smile:

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!

It it referring to folders in your project hierarchy.

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?

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

I got it. Thank you and many other folks!