Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Issue with dynamically generated mesh detection in unity build

Discussion in 'Scripting' started by dema3, May 6, 2023.

  1. dema3


    Sep 20, 2022
    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
    Code (CSharp):
    1. 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, 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!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class ConeGenerator : MonoBehaviour
    6. {
    7.     public float lengthToRadiusRatio;
    8.     public float scale;
    9.     public int vertexCount;
    10.     public Material coneMaterial;
    11.     public LayerMask targetMask;
    13.     Mesh coneMesh;
    14.     MeshFilter coneMeshFilter;
    15.     MeshRenderer coneRenderer;
    17.     void Awake()
    18.     {
    19.         //create child to hold mesh
    20.         GameObject coneVizualizer = new GameObject("ConeVizualizer");
    21.         coneVizualizer.transform.parent = this.transform;
    22.         coneVizualizer.transform.localPosition = Vector3.back * .001f;//offset form so boid is rendered on top of its fov. uses sphere collider so this is okay
    23.         coneVizualizer.transform.localRotation = Quaternion.identity;
    25.         //create mesh
    26.         coneMeshFilter = coneVizualizer.AddComponent<MeshFilter>();
    27.         coneRenderer = coneVizualizer.AddComponent<MeshRenderer>();
    28.         coneMesh = new Mesh();
    29. = "coneMesh";
    30.         coneMeshFilter.mesh = coneMesh;
    31.         coneRenderer.material = coneMaterial;
    33.         //set color
    34.         Color color = coneRenderer.material.color;
    35.         float colorIntensity = (color.r + color.g + color.b) / 3f;
    36.         float factor = colorIntensity + Random.Range(colorIntensity * .7f, colorIntensity * 1.7f);
    37.         coneRenderer.material.color = new Color(color.r, color.g * factor, color.b, color.a);
    39.         GenerateConeMesh();
    40.         coneVizualizer.AddComponent<MeshCollider>();
    41.         //thank you
    42.         coneVizualizer.layer = (int) Mathf.Log(targetMask.value, 2);
    43.     }
    45.     void GenerateConeMesh()
    46.     {
    47.         coneMesh.Clear();
    48.         coneMesh.vertices = new Vector3[]
    49.         {
    50.             Vector3.up * scale,
    51.             (Vector3.right * 1/lengthToRadiusRatio - Vector3.up) * scale,
    52.             (Vector3.left * 1/lengthToRadiusRatio - Vector3.up) * scale
    53.         };
    54.         coneMesh.triangles = new int[]
    55.         {
    56.             0, 1, 2
    57.         };
    58.         coneMesh.RecalculateNormals();
    59.     }
    60. }
  2. dema3


    Sep 20, 2022
    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

    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.
    aoi00115 likes this.
  3. aoi00115


    May 9, 2022
    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.
  4. Kurt-Dekker


    Mar 16, 2013
    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:

    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:

    - 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.