No suitable method found to override

Hello. I tried to make a new environment for ML-Agents, but after writing a code, the errors happened :
‘AgentControl.Heuristic(float[ ])’: no suitable method found to override
‘AgentControl.OnActionReceived(float[ ])’: no suitable method found to override

I’m using Unity 2019.4, ML-Agents 2.0.0 exp-1.
My code is below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;

public class AgentControl : Agent
{
    public Transform target1;
    public Transform target2;
    public Transform target3;
    public Transform target4;
    Rigidbody rBody;
    private bool onEpisode = false;
    private string closestTarget;

    public override void Initialize()
    {
        this.rBody = GetComponent<Rigidbody>();
        onEpisode = false;
    }

    public override void OnEpisodeBegin()
    {
        this.rBody.angularVelocity = Vector3.zero;
        this.rBody.velocity = Vector3.zero;
        this.transform.rotation = Quaternion.identity;

        this.transform.localPosition = new Vector3(Random.Range(-14.0f, 14.0f), 0.5f, Random.Range(-9.0f, 9.0f));
        onEpisode = true;

        float dis1 = Vector3.Distance(this.transform.localPosition, target1.localPosition);
        float dis2 = Vector3.Distance(this.transform.localPosition, target2.localPosition);
        float dis3 = Vector3.Distance(this.transform.localPosition, target3.localPosition);
        float dis4 = Vector3.Distance(this.transform.localPosition, target4.localPosition);

        float leastDis = Mathf.Min(dis1, dis2, dis3, dis4);

        if (leastDis == dis1) closestTarget = "Target1";
        if (leastDis == dis2) closestTarget = "Target2";
        if (leastDis == dis3) closestTarget = "Target3";
        if (leastDis == dis4) closestTarget = "Target4";
    }

    public override void OnActionReceived(float[] vectorAction)
    {
        Vector3 dirToGo = Vector3.zero;
        Vector3 rotateDir = Vector3.zero;

        int action = (int)vectorAction[0];
        if (action == 1) dirToGo = transform.forward;
        if (action == 2) dirToGo = transform.forward * -1.0f;
        if (action == 3) rotateDir = transform.up;
        if (action == 4) rotateDir = transform.up * -1.0f;

        this.transform.Rotate(rotateDir, Time.deltaTime * 200f);
        this.rBody.AddForce(dirToGo * 0.5f, ForceMode.VelocityChange);

        if (this.transform.localPosition.y < 0)
        {
            AddReward(-0.3f);
            EndEpisode();
        }

        AddReward(-1.0f / MaxStep);
    }

    public void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "target")
        {
            if (onEpisode)
            {
                if (collision.gameObject.name == closestTarget)
                {
                    AddReward(1.0f);
                    EndEpisode();
                }

                else
                {
                    AddReward(0.3f);
                    EndEpisode();
                }

            }
            else
            {
                this.transform.localPosition = new Vector3(Random.Range(-14.0f, 14.0f), 0.5f, Random.Range(-9.0f, 9.0f));
            }
        }
    }

    public override void Heuristic(float[] actionsOut)
    {
        actionsOut[0] = 0;
        if (Input.GetKey(KeyCode.UpArrow)) actionsOut[0] = 1;
        if (Input.GetKey(KeyCode.DownArrow)) actionsOut[0] = 2;
        if (Input.GetKey(KeyCode.RightArrow)) actionsOut[0] = 3;
        if (Input.GetKey(KeyCode.LeftArrow)) actionsOut[0] = 4;
    }
}

Could you help me?

Please read the migration guide when upgrading the ml-agents version. You will see that these method signatures have changed on Agent:

  • Agent.CollectDiscreteActionMasks(DiscreteActionMasker actionMasker) --> Agent.WriteDiscreteActionMask(IDiscreteActionMask actionMask)
  • Agent.Heuristic(float[ ] actionsOut) --> Agent.Heuristic(in ActionBuffers actionsOut)
  • Agent.OnActionReceived(float[ ] vectorAction) --> Agent.OnActionReceived(ActionBuffers actions)
  • Agent.GetAction() --> Agent.GetStoredActionBuffers()
4 Likes

Thank you! I understand “override” is no longer used in this method.
This is the first time ML-Agents upgrade for me, so I will check there next time.

1 Like

hello, i’ve changed 2 lines

  • Agent.Heuristic(float[ ] actionsOut) --> Agent.Heuristic(in ActionBuffers actionsOut)
  • Agent.OnActionReceived(float[ ] vectorAction) --> Agent.OnActionReceived(ActionBuffers actions)th

these 2 errors appear:

Assets\Hummingbird\Scripts\HummingbirdAgent.cs(116,43): error CS0246: The type or namespace name ‘ActionBuffers’ could not be found (are you missing a using directive or an assembly reference?)

Assets\Hummingbird\Scripts\HummingbirdAgent.cs(192,39): error CS0246: The type or namespace name ‘ActionBuffers’ could not be found (are you missing a using directive or an assembly reference?)

Any help please?

1 Like

Those specific errors should be fixed by adding this using directive at the top of the file:

using Unity.MLAgents.Actuators;

Are you migrating the Hummingbird project to ML-Agents 2.0? There’s probably quite a bit of work required to do that, you might be better off using ML-Agents 1.0 if you want to use that project.

1 Like

Thank you

Thank you!
I just faced the same issues, and just fixed it by downgrading the package to https://github.com/Unity-Technologies/ml-agents/releases/tag/release_1

it says “Cannot apply indexing with [ ] to an expression of type ‘ActionBuffers’” I only used [ ] in actionsOut[0] and actionsOut[1]. send help please.

Hello everyone!

Versions:
ml-agents (python package): 0.28.0
ml-agents-envs (python package): 0.28.0
Unity Package Manager package: ML Agents 2.01

Errors #1:
Assets\Scripts\BounceAgent.cs(30,25): error CS0115: 'BounceAgent.OnActionReceived(float[ ])': no suitable method found to override
&
Assets\Scripts\BounceAgent.cs(49,25): error CS0115: ‘BounceAgent.Heuristic(float[ ])’: no suitable method found to override
=========================================================================================
I understand that this code is deprecated - but when I change it to OnActionReceived(ActionBuffers actions) or Heuristic(in ActionBuffers actionsOut), I get the following error:
Error CS0021 Cannot apply indexing with [ ] to an expression of type ‘ActionBuffers’
This is because I have not declared an array - but when I do declare an array, I get the following errors:
Error CS1503 Argument 1: cannot convert from ‘Unity.MLAgents.Actuators.ActionBuffers’ to ‘float’
&
Error CS0029 Cannot implicitly convert type ‘Unity.MLAgents.Actuators.ActionBuffers’ to ‘float’
&
Error CS0019 Operator ‘*’ cannot be applied to operands of type ‘ActionBuffers’ and ‘ActionBuffers’
I don’t know how to change ActionBuffers to a float…(or if that would help)…
I tried downgrading my packages, but after 3 days I ended up getting everything back, so I’m not going to use that option. Here is a screenshot of the problematic code…


Any help would be much appreciated!

Any details about how to downgrade to ML agents 1.0 would be appreciated. Also does it need to be 1.0 or can be e.g. 1.5 etc. Thanks in advance.

using UnityEngine;
using UnityEngine.SceneManagement;

public class MainMenu : MonoBehaviour
{
    public void Play()
    {
        SceneManager.LoadScene(1);
    }

    public void Options()
    {
        SceneManager.LoadScene(2);
        Debug.Log("You Open Setings");
    }

    public void Exit()
    {
        Application.Quit();
    }

    public void ExitSettings()
    {
        SceneManager.LoadScene(0);
    }
     
    public void Esc()
    {
        if(Input.GetKeyDown(Esc))
        SceneManager.LoadScene(3);

hello my code see like this and error see like this:

Assets_Scripts\MainMenu.cs(29,29): error CS1503: Argument 1: cannot convert from ‘method group’ to ‘KeyCode’

PLease Help

if(Input.GetKeyDown(Esc))
it doesn’t know what “Esc” is, you need to put
if (Input.GetKeyDown(KeyCode.Escape))

Go to your project → open project manager → press the “Add” button on the top left (+ sign) → press “Add package from git URL” → paste github link