Hi,
I’m trying to get access to variables from another script in C#
in javascript i’d use:
variable = GameObject.Find(“myobject”).GetComponent(“myscript”).othervariable;
but this doesn’t seem to work. In c# it says:
UnityEngine.Component does not contain a definition for othervariable
Thanks for any help you can provide
Try (untested, but you should get the idea):
variable = (GameObject.Find("myobject").GetComponent(typeof(myscript)) as myscript).othervariable;
GetComponent returns object of type Component. Because of that you get it in correct type. You could also cast it:
variable = ((myscrip)GameObject.Find("myobject").GetComponent(typeof(myscript))).othervariable;
I would not use string version of GetComponent as it is slower than alternative one. In addition, with typeof() compiling fails if there is something wrong (e.g., typo in class name).
If myscript is javascript. You must put it in precompile step. Otherwise you can’t access it in c# script.
oh, this sounds interesting. I thought it is not possible at all to share one and the same Variables across JavaScript and C# scripts.
Can you briefly describe what you mean by put it in precompile step? Is that difficult. (I am very new to programming)
You can’t share them.
You can only expose them from JS to C# or from C# to JS, but knowledge of the other class will never work in both directions.