Changing variable of another script under #pragma strict

I am having many problems trying to change variables on script components on other objects under pragma strict. this is a script that works on an android tablet i want to be abel to change the settings on the ‘DragOrbit.cs’ script attached to the main camera.

var camOverview: GameObject;   // main camera assigned to this variable

if(GUI.Button(Rect(10,10,150,50), "Cam orbit change")){
		    		
	var dragorbit : MonoBehaviour = camOverview.GetComponent("DragOrbit") as MonoBehaviour;
		
		dragorbit.enabled = !tbddragorbit.enabled;   //switches script on and off
		Debug.Log(tbddragorbit.);	// returns Main Camera (DragOrbit)
	}

this script works and manages to switch the script off and on but i cant get it to access the target or distance variables which are in the dragorbit script by using

dragorbit.target = 60;

i get an error message saying

'target' is not a member of 'UnityEngine.MonoBehaviour'

i have tried to set the type of the component variable to itself

var dragorbit : DragOrbit = camOverview.GetComponent("DragOrbit") as DragOrbit;

but then i get this error

BCE0018: The name 'DragOrbit' does not denote a valid type ('not found').

Would really appreciate any help on this.

I notice your DragOrbit is Csharp and this script is JavaScript. With the BCE0018 error, it looks like a compilation order problem to me. Are both scripts in the same folder? Are they in the Plugins or Standard Assets folder?

i dont think its compilation order but a type error - i can only GetComponent as a monobehaviour i cannot set the getcomponent class as the script class. i have tried to put both scripts in seperate and plugin folders but it still doesn't work

A type error is returned, yes, but only because the compilation order have your JavaScript unable to read your CSharp class. Putting your CSharp in the Plugins have them compiled earlier (ready for JavaScript). With that done, you may finally cast your variable as a DragOrbit type and access whatever property you need. That's why mixing programming languages can ruin your day.

Thats done it BY0LOG1C I understand it now and it works. THANKS! Could you post that as an answer so i can mark it as answered.

2 Answers

2

You declared the dragorbit object as “MonoBehaviour”, but you try to access a variable that is part of the “DragOrbit” class instead. When you use typed variables, the compiler will check methods and variables against the declared type, not the actual runtime type.

change:

var dragorbit : MonoBehaviour

to

var dragorbit : DragOrbit 

and you will have more luck.

as i have stated above this throws the error BCE0018: The name 'DragOrbit' does not denote a valid type ('not found').

CS and JS scripts can’t see each other at compile time because the compilers are different, but you can work around this placing your scripts in a folder outside Standard Assets: Standard Assets scripts are compiled first, what make their CIL code available to scripts of any language compiled in subsequent “waves” (the scripts outside Standard Assets are compiled last - read this doc).

If you still get this error after making your scripts compile last, try to comment out the pragma strict line - maybe some 3.5 bug in pragma strict is causing the problem.

NOTE: It’s better to use the typed version of GetComponent - it’s faster and returns the desired type:

var dragorbit : DragOrbit = camOverview.GetComponent(DragOrbit);

both scripts are not in the standard assets folder. what i dont understand is that i can disable the component but not change teh variables

You can disable the component because you declared it as MonoBehaviour, the base class that contains the enabled variable - but MonoBehaviour doesn't have the distance variable, which is declared in DragOrbit.cs. You must move DragOrbit.cs to Standard Assets or Plugins, so it will be compiled first and become visible to your JS script.

ok the disabling makes sense now, I moved the cs script to plugins and it still says the dragorbit.target is not a member of 'UnityEngine.MonoBehaviour'

Make the variable type 'DragOrbit' again. It should work now that your CSharp file is compiled earlier (within the Plugins folder).

Forget about MonoBehaviour: declare the variable and the getComponent types as DragOrbit, like this: var dragorbit : DragOrbit = camOverview.GetComponent(DragOrbit); DragOrbit also has an enabled variable, which it inherited from MonoBehaviour