ml-agents icon indicating copy to clipboard operation
ml-agents copied to clipboard

error CS0122: 'ModelAssetData' is inaccessible due to its protection level

Open Juan-Sebastian-Silva opened this issue 1 year ago • 2 comments

I already have the correct installation since it follows the steps correctly and I don't get any errors. But when I try to import the "3D ball" scene to Unity I get the following error

Assets\ML-Agents\Examples\SharedAssets\Scripts\ModelOverrider.cs(306,68): error CS0122: 'ModelAssetData' is inaccessible due to its protection level.

  • Unity Version: 2023.2.0f1
  • OS + version: Windows 10
  • ML-Agents version: branch 21
  • Torch version: 2.1.1
  • Environment: 3D ball
  • Python 3.10.12

Error

I would appreciate the help, I have tried many things but I have no ideas left.

Juan-Sebastian-Silva avatar Sep 27 '24 22:09 Juan-Sebastian-Silva

Hi, has anyone found a solution yet? I'm facing the same issue.

jash-maester avatar Oct 07 '24 20:10 jash-maester

    ModelAsset LoadSentisModel(byte[] rawModel)
        {
            
            var asset = ScriptableObject.CreateInstance<ModelAsset>();
           
            var modelAssetDataType = typeof(ModelAsset).Assembly.GetType("NamespaceOfModelAssetData.ModelAssetData"); 
            var modelAssetDataInstance = ScriptableObject.CreateInstance(modelAssetDataType);
     
            FieldInfo modelAssetDataField = typeof(ModelAsset).GetField("modelAssetData", BindingFlags.NonPublic | BindingFlags.Instance);

            if (modelAssetDataField != null)
            {
                modelAssetDataField.SetValue(asset, modelAssetDataInstance);

                FieldInfo valueField = modelAssetDataType.GetField("value", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                if (valueField != null)
                {
                    valueField.SetValue(modelAssetDataInstance, rawModel);
                }
                else
                {
                    Debug.LogError("Failed to retrieve the value field from ModelAssetData.");
                }
            }
            else
            {
                Debug.LogError("Failed to retrieve modelAssetData field from ModelAsset using reflection.");
            }

            return asset;
        }

The above code works for me, Find the ModelAsset LoadSentisModel Method in the project file: project\Assets\ML-Agents\Examples\SharedAssets\Scripts\ModelOverrider.cs

also add the using System.Reflection;

After this replace the method with this code and you should get a working sample. The sample seems to work as intended. Hope this helps!

powerman984 avatar Oct 08 '24 10:10 powerman984

    ModelAsset LoadSentisModel(byte[] rawModel)
        {
            
            var asset = ScriptableObject.CreateInstance<ModelAsset>();
           
            var modelAssetDataType = typeof(ModelAsset).Assembly.GetType("NamespaceOfModelAssetData.ModelAssetData"); 
            var modelAssetDataInstance = ScriptableObject.CreateInstance(modelAssetDataType);
     
            FieldInfo modelAssetDataField = typeof(ModelAsset).GetField("modelAssetData", BindingFlags.NonPublic | BindingFlags.Instance);

            if (modelAssetDataField != null)
            {
                modelAssetDataField.SetValue(asset, modelAssetDataInstance);

                FieldInfo valueField = modelAssetDataType.GetField("value", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                if (valueField != null)
                {
                    valueField.SetValue(modelAssetDataInstance, rawModel);
                }
                else
                {
                    Debug.LogError("Failed to retrieve the value field from ModelAssetData.");
                }
            }
            else
            {
                Debug.LogError("Failed to retrieve modelAssetData field from ModelAsset using reflection.");
            }

            return asset;
        }

The above code works for me, Find the ModelAsset LoadSentisModel Method in the project file: project\Assets\ML-Agents\Examples\SharedAssets\Scripts\ModelOverrider.cs

also add the using System.Reflection;

After this replace the method with this code and you should get a working sample. The sample seems to work as intended. Hope this helps!

Worked for me, thanks!

J0hnnyGee avatar Oct 25 '24 08:10 J0hnnyGee

Worked for me too! Thankkkkk!

Jarain001 avatar Oct 26 '24 12:10 Jarain001

J0hnnyGee , Thank you. your code worked!

synosyno8 avatar Nov 02 '24 08:11 synosyno8

Worked for me, too. Thank you!

haedam19 avatar Nov 26 '24 05:11 haedam19

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Dec 26 '24 08:12 github-actions[bot]

Using reflection does solve the problem, but it's obviously not the normal solution, and since ModelAssetData became an internal variable with one of the sentis updates, sentis should have provided some other way of updating this variable at the same time, or else I can't figure out what the purpose of this change was

vipcxj avatar Jan 02 '25 13:01 vipcxj

    ModelAsset LoadSentisModel(byte[] rawModel)
        {
            
            var asset = ScriptableObject.CreateInstance<ModelAsset>();
           
            var modelAssetDataType = typeof(ModelAsset).Assembly.GetType("NamespaceOfModelAssetData.ModelAssetData"); 
            var modelAssetDataInstance = ScriptableObject.CreateInstance(modelAssetDataType);
     
            FieldInfo modelAssetDataField = typeof(ModelAsset).GetField("modelAssetData", BindingFlags.NonPublic | BindingFlags.Instance);

            if (modelAssetDataField != null)
            {
                modelAssetDataField.SetValue(asset, modelAssetDataInstance);

                FieldInfo valueField = modelAssetDataType.GetField("value", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                if (valueField != null)
                {
                    valueField.SetValue(modelAssetDataInstance, rawModel);
                }
                else
                {
                    Debug.LogError("Failed to retrieve the value field from ModelAssetData.");
                }
            }
            else
            {
                Debug.LogError("Failed to retrieve modelAssetData field from ModelAsset using reflection.");
            }

            return asset;
        }

The above code works for me, Find the ModelAsset LoadSentisModel Method in the project file: project\Assets\ML-Agents\Examples\SharedAssets\Scripts\ModelOverrider.cs

also add the using System.Reflection;

After this replace the method with this code and you should get a working sample. The sample seems to work as intended. Hope this helps!

Worked for me!! Thank you so much!

tmpoulionis avatar Feb 09 '25 15:02 tmpoulionis

Also add: using Debug = UnityEngine.Debug;

IvanChen-ctrl avatar Feb 23 '25 04:02 IvanChen-ctrl

Really helpful!! Thank you

wobudaoa avatar Mar 21 '25 10:03 wobudaoa

Really helpful, thanks a lot!

Yorklandian avatar Apr 02 '25 08:04 Yorklandian

Thank you!!!

xixixi2615 avatar Jun 17 '25 09:06 xixixi2615

Worked for me! Thanks!

Litten0 avatar Jun 24 '25 16:06 Litten0