Issue with dynamically generated mesh detection in unity build

TLDR: Physics.OverlapSphere does not detect dynamically generated mesh/collider in webGL build.

I am messing around with boids and have a fun little simulation running in unity. In the editor everything works, however, when I create a webGL build only select parts of the boid behavior works. The boid behavior that is independent of other boids, like obstacle avoidance, works. The boid behavior that is dependent on neighboring boids does not. I've figured out that this is because the boids aren't able to detect eachother.

Each boid generates a list of visible targets (neighboring boids) as follows

Collider[] targetsInViewRadius = Physics.OverlapSphere(transform.position, viewRadius, boidMask);

This works in the editor, but in the build targetsInViewRadius is never populated. I believe I have narrowed the issue down to Physics.OverlapSphere not picking up the dynamically generated mesh/collider I am creating for each boid. Plus I've never tried mesh generation before so my best guess is that the mistake is there.

I've looked at a couple posts with similar issues that stemmed from mesh.isReadable being false. But from https://docs.unity3d.com/ScriptReference/Mesh-isReadable.html, t "A dynamic Mesh created from script always returns true" which I doubled checked. I'm still pretty new to unity and have no clue what the issue is or where to look next so I've included my mesh generation bellow. Any help would be greatly appreciated! Thanks!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ConeGenerator : MonoBehaviour
{
    public float lengthToRadiusRatio;
    public float scale;
    public int vertexCount;
    public Material coneMaterial;
    public LayerMask targetMask;

    Mesh coneMesh;
    MeshFilter coneMeshFilter;
    MeshRenderer coneRenderer;

    void Awake()
    {
        //create child to hold mesh
        GameObject coneVizualizer = new GameObject("ConeVizualizer");
        coneVizualizer.transform.parent = this.transform;
        coneVizualizer.transform.localPosition = Vector3.back * .001f;//offset form Vector3.zero so boid is rendered on top of its fov. uses sphere collider so this is okay
        coneVizualizer.transform.localRotation = Quaternion.identity;

        //create mesh
        coneMeshFilter = coneVizualizer.AddComponent<MeshFilter>();
        coneRenderer = coneVizualizer.AddComponent<MeshRenderer>();
        coneMesh = new Mesh();
        coneMesh.name = "coneMesh";
        coneMeshFilter.mesh = coneMesh;
        coneRenderer.material = coneMaterial;

        //set color
        Color color = coneRenderer.material.color;
        float colorIntensity = (color.r + color.g + color.b) / 3f;
        float factor = colorIntensity + Random.Range(colorIntensity * .7f, colorIntensity * 1.7f);
        coneRenderer.material.color = new Color(color.r, color.g * factor, color.b, color.a);

        GenerateConeMesh();
        coneVizualizer.AddComponent<MeshCollider>();
        //thank you https://answers.unity.com/questions/1288179/layer-layermask-which-is-set-in-inspector.html
        coneVizualizer.layer = (int) Mathf.Log(targetMask.value, 2);
    }

    void GenerateConeMesh()
    {
        coneMesh.Clear();
        coneMesh.vertices = new Vector3[]
        {
            Vector3.up * scale,
            (Vector3.right * 1/lengthToRadiusRatio - Vector3.up) * scale,
            (Vector3.left * 1/lengthToRadiusRatio - Vector3.up) * scale
        };
        coneMesh.triangles = new int[]
        {
            0, 1, 2
        };
        coneMesh.RecalculateNormals();
    }
}

Was able to fix the problem. Turns out it did not have to do with my mesh generation or Physics.OverlapSphere. The issue stemmed from some connection on the instantiated boids that Unity editor adds automatically when instantiating, whereas the build does not. From this post https://answers.unity.com/questions/688926/collisions-not-working-when-game-built-but-works-f.html

To fix the issue I used one of the instantiated boids from the editor as the boid prefab. What ever connection was missing was preserved and it now works great. Although I would love to have a better understanding of this issue to avoid it in the future.

1 Like

I'm having the same exact issue, except it's not the object instantiated in-game that's not detected by the overlap sphere but the objects scattered around in the scene beforehand. I thought your solution would also help me get out of this sticky situation but nah, it did not work.
The project I'm working on is a simple RPG game where there's a player and enemies scattered around the map. I coded it so that the player detects enemies within the vicinity with the overlap sphere.
So here's what I did. I made a prefab out of the player object in-game so that it'd also save something you mentioned, which is added automatically upon running and lets the overlap sphere detect other colliders. Then I went ahead and replaced the player with the new prefab.
It works when it's built in the editor though, when I build it with the webGL, the overlap sphere does not seem to work at all.
It'd be greatly appreciated if you could share your insight on this.

[quote=“aoi00115”, post:3, topic: 917084]
I’m having the same exact issue, except it’s not the object instantiated in-game that’s not detected by the overlap sphere but the objects scattered around in the scene beforehand.
[/quote]

Then it isn’t really the “exact issue.”

In any case, please don’t necro-post. If you have a new question, make a new post. It’s FREE!!

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

  • Do not TALK about code without posting it.
  • Do NOT post unformatted code.
  • Do NOT retype code. Use copy/paste properly using code tags.
  • Do NOT post screenshots of code.
  • Do NOT post photographs of code.
  • ONLY post the relevant code, and then refer to it in your discussion.