I have the following setup:
- Write my C++ Physics Engine code
- Generate a C# interface to the C++ Physics Engine using SWIG
- Include the C# .DLL and C# source code autogenerated from SWIG in the Plugins folder under Assets for my Unity project
- I am using 2022.3.18f version of Unity for my Unity project
- When I try to use a C# class from SWIG generated C# source code (let’s call it ApseEvent), I am getting a CS0119 error with the message " ‘ApseEvent’ is a type, which is not valid in the given context [CS0119]"
I would attach the source code here, but the messaging system is not allowing me to upload any files because I am a new user.
Has anyone else run into this sort of an issue already? What am I missing in my setup?
Thanks
The error you’re encountering (CS0119: “‘ApseEvent’ is a type, which is not valid in the given context”) could be caused by several issues. Here are some potential solutions and steps to debug the problem:
- Naming Conflicts
The class ApseEvent might be conflicting with other types in your code or Unity’s namespaces. Ensure you’re referencing it with the correct namespace, if applicable:
using YourNamespace;
var apseEvent = new YourNamespace.ApseEvent();
- Issues with SWIG-Generated Code
SWIG sometimes generates C# code that may not fully align with Unity’s requirements. Check the autogenerated C# code for ApseEvent to verify there are no syntax errors or unexpected declarations.
- Native DLL Loading Issues
If ApseEvent interacts with a native DLL, ensure the DLL is:
- Placed in the correct folder (
Assets/Plugins/x86_64/ for 64-bit or x86/ for 32-bit).
- Compatible with your project’s target architecture (e.g., x64 or x86).
- Includes all required dependencies.
- Unity or SWIG Version Compatibility
Verify that your Unity version (2022.3.18f) is compatible with the version of SWIG you’re using. Updating Unity to the latest LTS version or regenerating the SWIG code might help.
Thanks for the response. After much review of my code, the problem turned out to be this line:
ApseEvent event = new ApseEvent();
Apparently, in Unity, where my client script is inheriting from MonoBehavior class, event is a reserved keyword.
However, I am still not able to use my plugin (xxx.dll) because it is not able to load the following dependent libraries:
MSVCP140D.dll
VCRUNTIME140D.dll
VCRUNTIME140_1D.dll
ucrtbased.dll
KERNEL32.dll
My question here is, how do I find out which PATH Unity is looking at to load all the dependencies of my DLL (xxx.dll)? How do I find that out?
Thanks.