Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by malinovsky239 · Dec 05, 2017 at 02:36 PM · meshesmeshrenderermeshfilter

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image malinovsky239 · Dec 05, 2017 at 10:59 PM 0
Share

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.

avatar image Buckslice malinovsky239 · Dec 06, 2017 at 03:02 AM 1
Share

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!

avatar image malinovsky239 Buckslice · Dec 06, 2017 at 02:53 PM 0
Share

Thank you very much! I managed to solve the issue!

As for your question,

How are you setting the color differently?

you can see _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?
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mesh coloring isnt working 1 Answer

Mesh between lines - Mesh.SetIndices 1 Answer

Generate Polygons and Colliders Runtime in 2D Game 0 Answers

【Help】how to get a model(MeshFilter, MeshRenderer) surface position? 1 Answer

Color not being applied properly to mesh 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges