2

I was using Unity 2019.1 and my project was running fine. After updating Unity to 2019.4 one of the scripts attached to a gameobject give me this warning: "The associated script can not be loaded" and I was not able to use the script.

I have no errors neither in the visual studio nor in Unity. After commenting and uncommenting codes from this script, I figured out what is making this problem: a variable from a library I am using in the project from dll file. (note that some variables from this dll work fine in my script and others are causing this problem).

I tried "Reimport all" and it didn't work.

Can you please help me to solve this problem?

Edit1:

I have no errors, just these 2 warnings: warning-1 warning-2

MBS
  • 673
  • 2
  • 16
  • 48
  • did you try to [clean up](https://stackoverflow.com/questions/56267842/cleaning-up-and-migrating-existing-unity-project-into-new-one-or-another-pc/56267992#56267992) your project? I guess we would need more details about the exact error message and the dll you are using – derHugo Aug 31 '20 at 09:21
  • Even with the cleanup, I get the same problem. I added images for the warnings I get. This is the dll I am using: https://github.com/opentok/opentok-unity-samples/blob/master/Assets/DLLs/OpenTokUnity.dll – MBS Aug 31 '20 at 11:58
  • You probably tried this already, but if not, try removing the component and adding it back to the object. – Seminix Aug 31 '20 at 12:10
  • @Seminix Sure I did this. I also create a new empty script and added it to the object. After adding one variable (private Session _session;) to this empty script it makes the same problem. – MBS Aug 31 '20 at 12:16
  • Can you decompile the code of the DLL to see if this variable is using native call? Via DllImportAttribute or other atributes? – Tomasz Juszczak Sep 09 '20 at 13:21

3 Answers3

1

I did some research on this stuff and now I do not really understand why is this happening. It happens only when you declare variables in MonoBehavior class, but if a variable is declared in a function or class which not inherits from MonoBehavior this works fine.

Although I can't find the reason, you can evade this problem by declaring a variable in another class for storing data.

Just like this:

using UnityEngine;
using OpenTok;

public class SceneScript : MonoBehaviour
{
    private SceneData data;
    
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

public class SceneData
{
    private Session session;
}
OnionFan
  • 479
  • 3
  • 10
  • This removed the warning but gives me an error when running the code: "TypeLoadException: Invalid type OpenTokSession for instance field SceneData:Session" – MBS Sep 09 '20 at 20:51
  • I also get these errors when running it on Android "Unity #0 0xc7a77801 (libunity.so) ? 0x0 Unity #1 0xc814f1e7 (libunity.so) ? 0x0 Unity #2 0xc7c622d9 (libunity.so) ? 0x0 Unity #3 0xc7c62247 (libunity.so) ? 0x0 Unity #4 0xde58bf5e (Unknown) ? 0x0" – MBS Sep 09 '20 at 21:53
1

I found there are some answers on this Forums

https://forum.unity.com/threads/general-problem-with-scripts-the-associated-script-cannot-be-loaded.185994/

You should check it out

Yash Vakil
  • 71
  • 7
0

It looks like you have or deleted or moved the file. the easiest way to fix this would be to remove and add the component to the object. if you get an error run the automatic API updater.

how to run the API updater: go to the top of your screen > assets > Run API Updater

Run API Updater Picture!!!

Jadon Wolfgang
  • 180
  • 1
  • 12
  • Run Api Updater is disabled. How to run it? – MBS Sep 09 '20 at 20:49
  • have you tried readding the script? because that could be a problem. – Jadon Wolfgang Sep 10 '20 at 13:11
  • Try making a backup and delete all the scripts and import them.(just the scripts that arent working). then try it again. it could be that your scripts are in a later VS state so reimporting them should rebuild them to the current version. – Jadon Wolfgang Sep 10 '20 at 13:53
  • Then you will have to make a backup of the scripts and reimport them or copy and paste them. that is the best option in my opinion – Jadon Wolfgang Sep 10 '20 at 14:19
  • I also did this. The problem is not with the file itself, it is from variables from dll. – MBS Sep 10 '20 at 14:40
  • okay this is going to be a bit wierd but copy all the contents of the script. create a new script and paste all the contents in there. now remove the old script component and add the new one – Jadon Wolfgang Sep 10 '20 at 14:49