Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Juan-Sebastian-Silva opened this issue Sep 27, 2024 · 7 comments
Labels
bug Issue describes a potential bug in ml-agents. stale Issues that have been idle for a while. Automatically closed by a bot if idle for too long.

Comments

@Juan-Sebastian-Silva
Copy link

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 Juan-Sebastian-Silva added the bug Issue describes a potential bug in ml-agents. label Sep 27, 2024
@jash-maester
Copy link

jash-maester commented Oct 7, 2024

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

@powerman984
Copy link

powerman984 commented Oct 8, 2024

    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!

@J0hnnyGee
Copy link

    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!

@Jarain001
Copy link

Worked for me too! Thankkkkk!

@synosyno8
Copy link

J0hnnyGee , Thank you. your code worked!

@haedam19
Copy link

Worked for me, too. Thank you!

Copy link

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

@github-actions github-actions bot added the stale Issues that have been idle for a while. Automatically closed by a bot if idle for too long. label Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue describes a potential bug in ml-agents. stale Issues that have been idle for a while. Automatically closed by a bot if idle for too long.
Projects
None yet
Development

No branches or pull requests

7 participants