Access C# variable in js script - c#

In the past I've accessed variables from other java scripts by doing
static var somevar;
In one script. And in the other:
OtherScript.somevar;
But I need to access a variable from C# code in a js script. I've tried:
Public static bool somevar;
In the C# file and in the js file:
OtherScript.somevar;
But I get the error: Unknown identifier 'somevar' in the js file

Just put your Unityscript file in the Plugin or in the Standard Assets folders.
Explanation:
In the "standard" way, you can only access a Unityscript variable from a C# script, but you cannot do the opposite. That's just C# code is compiled before Unityscript's one.
So, in order to achieve what you are trying to do, you must move your Unityscript file into a "special" folder (in Unity, they are the Plugin and the Standard Assets ones), so that it would be compiled at first.
After that, you should achieve the access with the usual logic.

You can't call a Static Object from JS and call its properties. You would have to get help from Unity GameObject to get the Object as Component as everything is an ObjectComponent in Unity. Then use that Component(Technically the Script) as your reference.
Example
GameObject name is "MyCsharpGameObject" <- the GameObject that has the Script
Then use.
theCscharpscript : OtherScript;
theCsharpscript = GameObject.find("MyCsharpGameObject").GetComponent("OtherScript");
On usage from the JS
theCsharpscript.thevariable;
Remember when using JS in MonoDevelop it doesn't give you Intellisence and that is just Normal. You can't however access a Static "Class" that is not attached to a GameObject.
if you insist on doing so. Then check Andrea's Answer. But his explanation might be complicated for you to work on.
But summing up the point that he was saying is. C# works in a NameSpace and JS is not part of that NameSpace so at lease you will have to somewhat include it in, and NameSpace is nothing more than a Folder Location in an Project. Simple as that.
However you must always remember to use PragmaStrict in this as it will always be transformed into a MonoDevelop calls even though you did not Extend from it.
Hope this help you.

Related

The type or namespace name "..." could not be found - Unity Vuforia

I am pretty new to Unity and Vuforia. I am trying to do solve this following problem. I have a script file that I am using for 2 different Image targets. Only 1 script should be running at a time. So i opened "DefaultObserverEventHandler.cs" to handle tracking events. I want to disable the script on that particular Image target when the tracking is lost. But when i try to get a reference on the script "QuizzBehaviour" i get an error. "QuizzBehaviour" is located in my Assets folder. Thanks in advance.
Ok, I fixed it. I made my own "DefaultObserverEventHandler.cs" script and placed it in the same folder where my "QuizzBehaviour.cs" script is.
"Find" will search through objects in scene not in your folders.However, at all costs you should avoid searching for objects by typing their name manually.In your case, the matter is quite simple. You can either add a script to an object and then use the get component command, or you can make an abstract class and refer to it.

Can't add script component because the script class cannot be found?

Yesterday I updated unity from unity5 to 2018.2.2f1. Unity scripts are not loading after Update 2018.2.2f1.
Once I try to play the Scene the scripts are not loaded and I can't add the script again it gives this error:
Can't add script component 'CubeScript' because the script class
cannot be found. Make sure that there are no compile errors and that
the file name and class name match.
If you still have the old copy of the project, upgrade the Unity project to Unity 2017 first then to 2018.2.2f1.
Here are the few possible reasons you may get this error(Ordered from very likely)
1.Script name does not match class name.
If script name is called MyClass, the class name must be MyClass. This is also case-sensitive. Double check to make sure that this is not the issue. To make sure that's not the issue, copy the class name and paste it as the script name to make sure that this is not the issue.
Note that if you have have multiple classes in one script, the class name that should match with the script name is the class that derives from MonoBehaviour.
2.There is an error in your script. Since this is an upgrade, there is a chance you're using an API that is now deprecated and removed. Open your script with Visual Studio and see if there is an error there then fix it. There is usually a red line under a code that indicates there is an error.
3.Bad import with the Unity importer and its automatic upgrade script.
Things to try:
A.The first thing to do is restart the Unity Editor.
B.Right click on the Project Tab then click "Reimport All"
C.If there is still issue, the only left is deleting the problematic script and creating a new one. There is an easier way to do this if the script is attached to many GameObjects in your scene.
A.Open the script, copy its content into notepad.
B.From the Editor and on the Project tab right click on the script "CubeScript", select "Find References In Scene".
C.Unity will now only show all the GameObjects that has this script attached to them. Delete the old script. Create a new one then copy the content from the notepad to this new script. Now, you can just drag the new script to all the filtered GameObject in the scene. Do this for every script effected. This is a manual work but should fix your issues when completed.
Here, is some reason why that's heaping.
1. Your Script Name And your class name isn't same.
2. May be there is an error in your script.
Here, is the few way to resolve it.
1. Make a same name of script and class in to this script.
2. find there is the error? and try to resolve it.
Posting here for historic purposes and if someone from Google ends up here, I strictly had the same problem (I'm on Windows 10) and here's how I fixed it :
If you click on the Console, you should see a blank error. Don't worry, it's a Editor bug, and you can see the Editor Log by selecting the console's window dropdown and selecting Open Editor Log.
For me, the problem was that "Tools\Roslyn\csc" could not be found, something that Unity uses to compile C# files.
I opened C:\Program Files\Unity\Hub\Editor\2019.2.14f1\Editor\Data\Tools\RoslynScripts\unity_csc.bat and I added ".exe" to the line "%APPLICATION_CONTENTS%\Tools\Roslyn\csc" /shared %*
(This line should now be : "%APPLICATION_CONTENTS%\Tools\Roslyn\csc.exe" /shared %*)
Now it works perfectly.
I'm completely new to Unity and game development, but I found that making the error true by entering an incorrect name, then entering the right one seems to "trick" Unity into fixing its error.
I had the same error show up but for different reasons (in case someone ends up here from google).
I was a beginner and named my file with a space in it. I got the same error after that. Make sure to NOT include spaces in your C# script.
One more thing which might help apart from other answers
Make the class which inherits from the MonoBehaviour the first class in the file. It fixed this issue for me.
Checking the console bugs and fix them is a good way to solve the problems "Can't add script component because the script class cannot be found?", although the bug seems to be irrelevant.
This works for me when I fix a bug in the other script.
for me I found out it was a script that I wasn't using which was linked to the player and other scripts that I deleted.
For me, the problem was that there was an error in another script.
Once I fixed the error, I could FINALLY attach my other scripts onto my gameobjects.
I just had this issue where I had a perfect script, no errors, and the same name as it should be but no matter what it wouldn't let me add it to a gameObject.
There was another script that was completely unrelated but it had an error in that and somehow that prevented me from adding my good new script.
Like some folks already posted here - the issue might be of a different code.
I was trying to add a script that was referenced by another one which had errors - it doesn't tell you that is the reason, although visual studio kinda suggests it by showing you the errors in the open script.
Just delete the bigger (errored) script and start adding to the unity project from the basic ones.
I had this issue where I had a good script, no errors, good name, restarting unity, following a tutorial exactly but still had this issue.
The way I fixed this is just deleting this buggy script file and creating it again. It fixed my problem perfectly! Now I could add the script to my game object with no such errors!
Hope this helps you! Cheers!

Programmatically import Asset in Hierarchy Unity

For a schoolproject we're working on a project for loading BIM-models into unity. We are working with .ifc files, which we convert to .dae for the time being. We created a script for importing the files using EditorUtility.OpenFilePanel. We are using IFCOpenshell's IFCConvert (a command line utility).
This goes well, and the Asset shows up in the Assets folder. The only thing left to do now, is importing the asset runtime into the Hierarchy *without dragging it into the scene or hierarchy *.
We've tried to use a WWW stream to get the file and assign AssetBundles to it and a few other methods, but no luck yet.
"The only thing left to do now, is importing the asset runtime into the Hierarchy"
Unless I misunderstand you: for items in "Project" (ie, your assets), if you want to put them in the current scene .. drag them in to the scene.
(ie, drag them either in to the Heirarchy panel or just literally in to the middle of the scene Editor.)
Are you saying you're trying to automate this ?? (I don't really see how you could - any button you added would be more difficult than .. just dragging them in.)
If you are simply asking how to load (at runtime) Resources, just look in to
Resources.LoadAll()
You'll learn that you have to keep the items in a "Resources/" folder and so on. It is well documented. There are 1000s of QA on the topic. Example code fragment
ra = Resources.LoadAll("VO/", typeof(AudioClip) ).Cast<AudioClip>().ToArray();
It is beyond the scope of a QA site to provide a tutorial on "Resources" but you can instantly find this on Unity3d.com.
Note that if, very simply, you just want to use a particular model in the build. Do nothing more than, drag it to the scene and place it where you want. Then in code just
public GameObject yourBuilding;
and use inspector-dragging to link that variable as you wish. Then you can use that variable as you wish. Note that for large complicated models, often you will go ahead and just have different inspector variables for the different parts, rather than even bothering to drill-down. (So, you might have "hospital", "eastWing" "corridor" .. etc. and just go ahead and "inspector-drag" those.)

make tool generate class file on unity, but cant use these file in time

I want to use Unity to make a game, but first I want to make a tool that does two things:
Generate class files.
Call these class files to execute some funciton.
e.g:
Generate a class file like this:
class TestClass{
public void print(){Debug.log("call this function")}
}
And execute this class:
#use reflection get this class instance,then call "print" function
MethondInfo printFunction = t.GetMethod("print");
printFunction.InvoKe()
The problem is, when generating the class, unityEdit can't find these files in time. I must, in Project view, chose the folder and right click "Show in Explorer", then unityEdit will add these new files to file system.
I think unity should provide some file API that does this, but I can't find it. What should I do?
When you add classes to the project by hand using File Explorer, on reentering UnityEditor it will automatically check for new or modified files. If you generate your classes using some kind of Editor script however, you must actively tell the Editor to check.
You can do this by calling AssetDatabase.Refresh from an Editor script, see http://docs.unity3d.com/ScriptReference/AssetDatabase.Refresh.html.

Using Python to Access Methods From C# Library - Interop .dll File

I have a .dll file (with "Interop." prefix) containing a library written in C#. Within the library is a class, several enums, several interfaces, and several delgates. (Observed by decompiling the .dll with JetBrains dotPeek)
See the dll structure here:
I need to use pure Python to access the methods within the class. I have tried:
from ctypes import *
name = "Interop.HTBRAILLEDRIVERSERVERLib.dll"
mydll = cdll.LoadLibrary(name)
However attempting to call any of the methods contained in the class "HtBrailleDriverClass" leads to "AttributeError: function 'initialize' not found". I have also attempted to access them from their ordinal indexes:
print mydll[1]
However this gives the error "AttributeError: function ordinal 1 not found".
Is anyone able to shed light on why I am unable to access the class within this .dll and why I cannot access any of the methods either?
Please bear in mind I must use pure Python.
You can use python.net to access it
import clr,sys
sys.path.append("path of your dll")
clr.AddReference("YourDllName")
import YourDllName
Then Try printing any value of member of your class like
print YourDllName.ClassName.Member
from your python script
Note : Your need to put clr.pyd and python.runtime.dll inside your
Python27/Dlls folder
If You don't want to append your dll path then put the dll inside the python2.x/Lib/site-packages folder .
Then u can avoid 2nd line - sys.path.append() too .

Categories