- Home /
[Solved] Two meshes work fine separately but can't be rendered simultaneously
I have a component that generates a mesh representing unit's field of view. Method Generate
is called inside the Awake()
method of the FieldOfView
component. I also have two different prefabs containing this component. Now the strange thing happens:
If I instantiate only one of these prefabs (no matter which one of two), its field of view is rendered properly.
If I instantiate both of them, neither one is rendered.
If I instatiate two copies of a single prefab, neither one is rendered.
If I instantiate both of them and start the game, then pause and switch
FieldOfView
component off in the inspector for one of them (again, no matter for which one of two), the mesh of the other game object is rendered.
Please, help me figure out what can be the reason for this awkward behavior. Please, see FieldOfView
component code below:
using UnityEngine;
namespace Assets.Scripts
{
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class FieldOfView : MonoBehaviour
{
private const int Steps = 10;
private const float HeightAboveGround = 0.5f;
[SerializeField] private float _barColorRed;
[SerializeField] private float _barColorGreen;
[SerializeField] private float _barColorBlue;
[SerializeField] private int _maxDistance;
[SerializeField] private float _fieldAngle;
private Mesh _mesh;
private void Awake()
{
Generate();
}
private void Generate()
{
GetComponent<MeshFilter>().mesh = _mesh = new Mesh();
Vector3[] vertices = new Vector3[Steps + 2];
vertices[0] = Vector3.zero;
int cnt = 1;
for (float angle = -_fieldAngle / 2; angle <= _fieldAngle / 2 + Mathf.Epsilon; angle += _fieldAngle / Steps)
{
float x = Mathf.Sin(angle * Mathf.Deg2Rad) * _maxDistance;
float y = Mathf.Cos(angle * Mathf.Deg2Rad) * _maxDistance;
vertices[cnt++] = new Vector3(x, HeightAboveGround, y);
}
_mesh.vertices = vertices;
Color barColor = new Color(_barColorRed, _barColorGreen, _barColorBlue);
Color[] colors = new Color[vertices.Length];
for (int i = 0; i < vertices.Length; i++)
{
colors[i] = barColor;
}
_mesh.colors = colors;
int[] triangles = new int[(Steps + 1) * 3];
for (int i = 0; i < Steps; i++)
{
triangles[i * 3] = 0;
triangles[i * 3 + 1] = i + 2;
triangles[i * 3 + 2] = i + 1;
}
_mesh.triangles = triangles;
_mesh.RecalculateBounds();
_mesh.RecalculateNormals();
}
public bool IsInField(Vector3 position)
{
Vector3 to = position - transform.position;
float distanceToTarget = to.magnitude;
float angle = Vector2.Angle(new Vector2(transform.forward.x, transform.forward.z), new Vector2(to.x, to.z));
return distanceToTarget < _maxDistance && Mathf.Abs(angle) < _fieldAngle / 2;
}
public void Die()
{
if (_mesh)
{
_mesh.Clear();
}
}
}
}
upd. Here is the link to a screencast on which you can see what exactly happens: https://drive.google.com/open?id=1jKpPbFDQXeXBtYPfZ8uNWe2k1OC_D4Fd
upd2. See the solution in the comments to the answer by Buckslice.
Answer by Buckslice · Dec 05, 2017 at 08:24 PM
I created a new project, made a prefab using your FieldOfView script, and instantiated a couple with another script. They all generated their meshes correctly with no problems. I had max distance and field angle set to 10. Though I had to look below the objects to see the meshes. Maybe that is your problem? In your shader for the field of view mesh you could add Cull Off if you don't know which side you will be looking at. Also make sure you aren't calling the Die() function earlier than you expect because that will clear the mesh.
Thank you for your comment. Unfortunately, it doesn't help. I can see each of the meshes in case if exactly one instance of FieldOfView
component is enabled at the moment. I also added debug output to the Die()
method and made sure that it isn't called.
I recorded my screen to show you what exactly happens. Here is the link to a gif file with the recording: https://drive.google.com/open?id=1j$$anonymous$$pPbFDQXeXBtYPfZ8uNWe2k1OC_D4Fd . I hope it may give you an idea of what else can be wrong.
Hmmm, well your code above seems fine at least. I think it is a problem elsewhere in your project. I see that both monsters are using the same 'Fire' material to render their field of view mesh. How are you setting the color differently? with $$anonymous$$aterialPropertyBlock or material.SetColor()? Try not setting the color, or try using a different material like the default standard shader to test. You could turn on the editor wireframe drawing mode too to see whats going on. Cool lookin game btw!
Thank you very much! I managed to solve the issue!
As for your question,
you can seeHow are you setting the color differently?
_mesh.colors = colors;
line in my code above. The issue was indeed caused by the usage of the same material. I added a single line
GetComponent<$$anonymous$$eshRenderer>().materials[0] = new $$anonymous$$aterial(Shader.Find("Particles/Additive"));
to the top of
Generate()
method, and now no matter how many units I instantiate, each of them has its own field of view. However, I still don't understand why this is happening, i.e. why can't I use the same material but set different colors for the mesh? Could you please explain?