#ML Agents assistance

1 messages · Page 1 of 1 (latest)

upper edge
#

I am making a project which requires ML Agents, but I am getting some errors while playing. If there is anyone who can please help us, DMs or open or you may ping me in the server.

green mauve
#

!code

waxen fogBOT
oak leaf
#

dm me

upper edge
#

ok hi im so sorry for the delay, i got a little busy elsewhere

#

it states shelter tag is not there but ive provided them

#

the code it redirected me to is this one

#

i took GPT's help for this one

#

someone please help me out w this simulation part T-T

fringe hinge
upper edge
fringe hinge
#

The errors themselves point to a missing/unassigned reference. But there's not much we can help you with without seeing the errors callstack.

upper edge
#

shall i send the git?

fringe hinge
# upper edge what shall i provide you with then?/

As I said several times already - the error/s callstack.
Click an error in the console - you will see more details in the section at the bottom. These are very important for understanding the errors. Copy the whole thing here.

upper edge
#

Oh sorry, i thought i provided the error callstack. I'm sending it.

upper edge
#

ok for starters, there is no movement in the game/scene view, after i press play

fringe hinge
upper edge
#

Fewer observations (0) made than vector observation size (16). The observations will be padded. UnityEngine.Debug:LogWarningFormat (string,object[]) Unity.MLAgents.Sensors.VectorSensor:Write (Unity.MLAgents.Sensors.ObservationWriter) (at ./Library/PackageCache/com.unity.ml-agents@0f918b8a6271/Runtime/Sensors/VectorSensor.cs:56) Unity.MLAgents.GrpcExtensions:GetObservationProto (Unity.MLAgents.Sensors.ISensor,Unity.MLAgents.Sensors.ObservationWriter) (at ./Library/PackageCache/com.unity.ml-agents@0f918b8a6271/Runtime/Communicator/GrpcExtensions.cs:400) Unity.MLAgents.RpcCommunicator:PutObservations (string,Unity.MLAgents.AgentInfo,System.Collections.Generic.List1<Unity.MLAgents.Sensors.ISensor>) (at ./Library/PackageCache/com.unity.ml-agents@0f918b8a6271/Runtime/Communicator/RpcCommunicator.cs:355)
Unity.MLAgents.Policies.RemotePolicy:RequestDecision (Unity.MLAgents.AgentInfo,System.Collections.Generic.List1<Unity.MLAgents.Sensors.ISensor>) (at ./Library/PackageCache/com.unity.ml-agents@0f918b8a6271/Runtime/Policies/RemotePolicy.cs:46) Unity.MLAgents.Agent:SendInfoToBrain () (at ./Library/PackageCache/com.unity.ml-agents@0f918b8a6271/Runtime/Agent.cs:1137) Unity.MLAgents.Agent:SendInfo () (at ./Library/PackageCache/com.unity.ml-agents@0f918b8a6271/Runtime/Agent.cs:1377) Unity.MLAgents.Academy:EnvironmentStep () (at ./Library/PackageCache/com.unity.ml-agents@0f918b8a6271/Runtime/Academy.cs:586) Unity.MLAgents.AcademyFixedUpdateStepper:FixedUpdate () (at ./Library/PackageCache/com.unity.ml-agents@0f918b8a6271/Runtime/Academy.cs:43)

fringe hinge
#

First, share code correctly(as big code blocks).

#

!code

waxen fogBOT
upper edge
#
using TMPro;

public class StormController : MonoBehaviour
{
    public GameObject[] trainingAreas;
    public Material calmStormMaterial;
    public Material moderateStormMaterial;
    public Material severeStormMaterial;
    
    private int currentStormLevel = 0;
    public TextMeshProUGUI statusText;

    void Start()
    {
        // Find all training areas if not assigned
        if (trainingAreas == null || trainingAreas.Length == 0)
        {
            trainingAreas = GameObject.FindGameObjectsWithTag("TrainingArea");
        }
        
        UpdateStatusText();
        DeactivateAllStorms(); // Start with no storm
    }

    public void ActivateCalmStorm()
    {
        currentStormLevel = 1;
        
        foreach (GameObject area in trainingAreas)
        {
            Transform storm = area.transform.Find("StormZone");
            if (storm != null)
            {
                storm.localScale = new Vector3(5, 0.1f, 5);
                storm.GetComponent<Renderer>().material = calmStormMaterial;
                storm.gameObject.SetActive(true);
            }
        }
        
        UpdateStatusText();
        Debug.Log("Calm Storm Activated");
    }
#
    public void ActivateModerateStorm()
    {
        currentStormLevel = 2;
        
        foreach (GameObject area in trainingAreas)
        {
            Transform storm = area.transform.Find("StormZone");
            if (storm != null)
            {
                storm.localScale = new Vector3(8, 0.1f, 8);
                storm.GetComponent<Renderer>().material = moderateStormMaterial;
                storm.gameObject.SetActive(true);
            }
        }
        
        UpdateStatusText();
        Debug.Log("Moderate Storm Activated");
    }

    public void ActivateSevereStorm()
    {
        currentStormLevel = 3;
        
        foreach (GameObject area in trainingAreas)
        {
            Transform storm = area.transform.Find("StormZone");
            if (storm != null)
            {
                storm.localScale = new Vector3(12, 0.1f, 12);
                storm.GetComponent<Renderer>().material = severeStormMaterial;
                storm.gameObject.SetActive(true);
            }
        }
        
        UpdateStatusText();
        Debug.Log("Severe Storm Activated");
    }

    public void DeactivateAllStorms()
    {
        currentStormLevel = 0;
        
        foreach (GameObject area in trainingAreas)
        {
            Transform storm = area.transform.Find("StormZone");
            if (storm != null)
            {
                storm.gameObject.SetActive(false);
            }
        }
        
        UpdateStatusText();
        Debug.Log("All Storms Deactivated");
    }

    void UpdateStatusText()
    {
        if (statusText != null)
        {
            string status = "Storm Level: ";
            switch (currentStormLevel)
            {
                case 0: status += "NONE"; break;
                case 1: status += "CALM"; break;
                case 2: status += "MODERATE"; break;
                case 3: status += "SEVERE"; break;
            }
            statusText.text = status;
        }
    }
}```
#

this is the first code i.e. StormController.cs

#
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;

public class StormAgentController : Agent
{
    public Transform stormZone;
    public Transform[] safeZones;
    public Material normalMaterial;
    public Material damagedMaterial;
    
    private Rigidbody rb;
    private Renderer agentRenderer;
    private float moveSpeed = 5f;
    private bool inStorm = false;
    private float stormDamageTimer = 0f;
    private Vector3 startPosition;

    public override void Initialize()
    {
        rb = GetComponent<Rigidbody>();
        agentRenderer = GetComponent<Renderer>();
        startPosition = transform.localPosition;
        
        // Find storm zone if not assigned
        if (stormZone == null)
        {
            GameObject stormObj = GameObject.Find("StormZone");
            if (stormObj != null)
                stormZone = stormObj.transform;
        }
            
        // Find safe zones if not assigned
        if (safeZones == null || safeZones.Length == 0)
        {
            GameObject[] safes = GameObject.FindGameObjectsWithTag("SafeZone");
            safeZones = new Transform[safes.Length];
            for (int i = 0; i < safes.Length; i++)
                safeZones[i] = safes[i].transform;
        }
    }
#
    {
        // Reset position randomly
        transform.localPosition = startPosition + new Vector3(
            Random.Range(-10f, 10f), 0, Random.Range(-10f, 10f));
        
        rb.linearVelocity = Vector3.zero;
        rb.angularVelocity = Vector3.zero;
        inStorm = false;
        stormDamageTimer = 0f;
        
        if (agentRenderer && normalMaterial)
            agentRenderer.material = normalMaterial;
    }
#
    {
        // Agent's position
        sensor.AddObservation(transform.localPosition);
        
        // Agent's velocity
        sensor.AddObservation(rb.linearVelocity);
        
        // Distance and direction to storm zone
        if (stormZone != null)
        {
            Vector3 toStorm = stormZone.position - transform.position;
            sensor.AddObservation(toStorm.normalized);
            sensor.AddObservation(toStorm.magnitude);
        }
        else
        {
            sensor.AddObservation(Vector3.zero);
            sensor.AddObservation(0f);
        }
        
        // Distance to nearest safe zone
        if (safeZones != null && safeZones.Length > 0)
        {
            float minDist = float.MaxValue;
            Vector3 nearestSafe = Vector3.zero;
            
            foreach (Transform safe in safeZones)
            {
                if (safe != null)
                {
                    float dist = Vector3.Distance(transform.position, safe.position);
                    if (dist < minDist)
                    {
                        minDist = dist;
                        nearestSafe = safe.position;
                    }
                }
            }
            
            Vector3 toSafe = nearestSafe - transform.position;
            sensor.AddObservation(toSafe.normalized);
            sensor.AddObservation(minDist);
        }
        else
        {
            sensor.AddObservation(Vector3.zero);
            sensor.AddObservation(0f);
        }
        
        // Is in storm?
        sensor.AddObservation(inStorm ? 1f : 0f);
    }
#
    {
        // Get actions
        float moveX = actions.ContinuousActions[0];
        float moveZ = actions.ContinuousActions[1];
        
        // Apply movement
        Vector3 move = new Vector3(moveX, 0, moveZ) * moveSpeed;
        rb.AddForce(move);
        
        // Small penalty for existing (encourages efficiency)
        AddReward(-0.001f);
        
        // Punish being in storm
        if (inStorm)
        {
            stormDamageTimer += Time.fixedDeltaTime;
            AddReward(-0.05f); // Continuous punishment
            
            if (agentRenderer && damagedMaterial)
                agentRenderer.material = damagedMaterial;
            
            // End episode if in storm too long
            if (stormDamageTimer > 5f)
            {
                AddReward(-1f);
                EndEpisode();
            }
        }
        else
        {
            stormDamageTimer = 0f;
            if (agentRenderer && normalMaterial)
                agentRenderer.material = normalMaterial;
        }
        
        // Fall off check
        if (transform.localPosition.y < -5f)
        {
            AddReward(-1f);
            EndEpisode();
        }
    }

    /*public override void Heuristic(in ActionBuffers actionsOut)
    {
        // Manual control for testing
        ActionSegment<float> continuousActions = actionsOut.ContinuousActions;
        continuousActions[0] = Input.GetAxis("Horizontal");
        continuousActions[1] = Input.GetAxis("Vertical");
    }*/

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Storm"))
        {
            inStorm = true;
        }
        else if (other.CompareTag("SafeZone"))
        {
            AddReward(0.1f);
        }
    }
#
    {
        if (other.CompareTag("Storm"))
        {
            inStorm = false;
            AddReward(0.5f);
        }
    }

    private void OnTriggerStay(Collider other)
    {
        if (other.CompareTag("SafeZone"))
        {
            AddReward(0.01f);
        }
    }
}```
#

This is the second code StormAgentController.cs

fringe hinge
#

Is the storm agent even present in the scene? Add some debug logs in the script and see if they print.