Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Chris12345 · Jul 05, 2015 at 09:39 PM · c#

Instantiating Mesh in edit mode? C#

Good Afternoon, I have been trying to build a mesh in edit mode.

Using MeshFilter.mesh in editor gives error

Instantiating mesh due to calling MeshFilter.mesh during edit mode. This will leak meshes

It recommends using MeshFilter.SharedMesh, I cannot use this as i want each mesh to be a separate object that can be manipulated.

Thanks Again for all your help.

Current Code

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 [ExecuteInEditMode]
 [RequireComponent (typeof (MeshFilter))]
 
 public class Generate : MonoBehaviour {
 
         private Mesh s;
 
         private Vector3[] vertices = {
         new Vector2(0,0),
         new Vector2(0,1),
         new Vector2(1,1),
         new Vector2(1,0)
         };
     
         private int[] triangles = {
         0,1,2,
         2,3,0
         };   
         
         public void Create()
         {
             MeshFilter meshfilter = gameObject.GetComponent<MeshFilter>();
            
                     s = meshfilter.mesh; //Error Line//
             
 
             s.name = "S";
             s.vertices = vertices;
             s.triangles = triangles;
         }
 }
 

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
1
Best Answer

Answer by karl_ · Jul 06, 2015 at 01:47 PM

Assuming your MeshFilter.sharedMesh field doesn't point to an already valid mesh, just assign s to it instead of the other way around.

GetComponent<MeshFilter>().sharedMesh = s;

Edit - full source:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 [ExecuteInEditMode]
 [RequireComponent (typeof (MeshFilter))]
 public class Generate : MonoBehaviour
 {
 
  public Color FillColor;
 
  private Mesh s;
 
  private Vector3[] vertices = {
      new Vector2(0,0),
      new Vector2(0,1),
      new Vector2(1,1),
      new Vector2(1,0)
  };
 
  private int[] triangles = {
      0,1,2,
      2,3,0
  };   
 
  public void Create()
  {
      s = new Mesh();
      GetComponent<MeshFilter>().sharedMesh = s;
 
      s.name = "S";
      s.vertices = vertices;
      s.triangles = triangles;
  }
 
  void Start()
  {
      Create();
  }
 
  void Update()
  {
     // if Create() hasn't been called yet, the mesh is still null
     if(s == null)
         return;
 
      Vector3[] vertice = s.vertices; //Error Line//
 
 
      Color[] colors = new Color[vertice.Length];
      int i = 0;
 
      while (i < vertice.Length)
      {
          colors[i] = FillColor;
          i++;
      }
 
      s.colors = colors;
  }
 }
Comment
Add comment · Show 13 · 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 Chris12345 · Jul 06, 2015 at 02:02 PM 0
Share

I had to modify the code to get it to work, but I am still getting an error?

 using UnityEngine;
  using UnityEditor;
  using System.Collections;
  
  [ExecuteInEdit$$anonymous$$ode]
  [RequireComponent (typeof ($$anonymous$$eshFilter))]
  
  public class Generate : $$anonymous$$onoBehaviour {
 
         public Color FillColor;
  
          private $$anonymous$$esh s;
  
          private Vector3[] vertices = {
          new Vector2(0,0),
          new Vector2(0,1),
          new Vector2(1,1),
          new Vector2(1,0)
          };
      
          private int[] triangles = {
          0,1,2,
          2,3,0
          };   
          
          public void Create()
          {
             
              s = GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh; //Fixed Line// :) 
              
  
              s.name = "S";
              s.vertices = vertices;
              s.triangles = triangles;
          }
         void Update() {
         Vector3[] vertice = s.vertices; //Error Line//

             NullReferenceException: Object reference not set to an instance of an object

         Color[] colors = new Color[vertice.Length];
         int i = 0;
         while (i < vertice.Length) {
             colors[i] = FillColor;
             i++;
         }
         s.colors = colors;
     }
  }
avatar image karl_ · Jul 06, 2015 at 02:15 PM 0
Share

I updated my answer with full source, you're just missing a constructor for the mesh s ( s = new $$anonymous$$esh() prior to assigning shared$$anonymous$$esh).

avatar image Chris12345 · Jul 06, 2015 at 02:23 PM 0
Share

Thanks for all your help, but I am getting an error at Vector3[] vertice = s.vertices; //Error Line// NullReferenceException: Object reference not set to an instance of an object

avatar image karl_ · Jul 06, 2015 at 02:28 PM 0
Share

Right, because you're not assigning s before using it. You need to add the s = new $$anonymous$$esh(); line before assigning it to the component, which again should be GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh = s; and not the other way around (as you have it). I updated my answer with a working example, make sure to pay attention to the order of assignments in Create(). Also note that you have to assign s to something valid prior to entering the Update method, otherwise you'll be accessing a null mesh. If you can't be guaranteed that s will be created prior to Update, consider adding a simple if(s == null) return; statement at the start of your Update function.

avatar image Chris12345 · Jul 06, 2015 at 02:37 PM 0
Share

I am now not getting any error's but my initial intension was to not use shared mesh as I would like to set the duplicate to another colour.

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Flip over an object (smooth transition) 3 Answers

Making a bubble level (not a game but work tool) 1 Answer


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