Trying to call a variable from another script in C#

I have been able to do this successfully with Unityscript by declaring

 var instanceName : ScriptName; 

and then by using instanceName.variable

but I haven’t had any luck in trying to accomplish the same thing in C#.

Any one able to help me with this?
Thanks

1 Answer

1

make sure you put ‘public’ before the declaration, eg

public ScriptName instanceName;

Thanks :) always forget about that. Now I have this error: <pre> An object reference is required to access non-static member </pre> in my update call I'm trying to set a variable <pre> string result = SearchField.stringToEdit; </pre> where SearchField.stringToEdit calls a string from the SearchField class.

Have you declared and assigned the SearchField variable?

never mind, the problem was I was calling the function of SearchField using SearchField.property rather than using the instance name of search.property Thanks guys :)

also worth noting is that when the stringToEdit is changed, the inspector still shows result as being blank.

Your "search" variable is a private variable, do you assign any reference to this variable? i guess not(if that's the whole script). You should either make it public and drag & drop the correct script instance onto the variable, or use GameObject.Find and GetComponent to get the desired script-instance via code.