Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 FusionSausage · Oct 11, 2013 at 05:08 PM · gameobjectmeshmousegridchange

Change mesh to to GameObject

Hi guys! I'm using a script to place objects on a grid that I found made by the community (thank you!) and it has a mesh that is drawn purple on the grid where the mouse is positioned. Now I want the mesh to instead of being just a solid color I want it to be a GameObject. How would I modify this code to make it a GameObject instead?

     private Mesh mesh;
      
     private Vector3[] verts;
     private Vector2[] uvs;
     private int[] tris;
      
      
     void ConstructMesh()
     {
         if ( !mesh )
         {
                mesh = new Mesh();
             MeshFilter f = GetComponent("MeshFilter") as MeshFilter;
             f.mesh = mesh;
             mesh.name = gameObject.name + "Mesh";
         }
      
         mesh.Clear();  
      
         verts = new Vector3[9 * 9]; 
         uvs = new Vector2[9 * 9];
         tris = new int[ 8 * 2 * 8 * 3];
      
         float uvStep = 1.0f / 8.0f;
      
         int index = 0;
         int triIndex = 0;
      
         for ( int z = 0; z < 9; z ++ )
         {
            for ( int x = 0; x < 9; x ++ )
            {
              verts[ index ] = new Vector3( x, 0, z );
              uvs[ index ] = new Vector2( ((float)x) * uvStep, ((float)z) * uvStep );
      
              if ( x < 8 && z < 8 )
              {
               tris[ triIndex + 0 ] = index + 0;
               tris[ triIndex + 1 ] = index + 9;
               tris[ triIndex + 2 ] = index + 1;
      
               tris[ triIndex + 3 ] = index + 1;
               tris[ triIndex + 4 ] = index + 9;
               tris[ triIndex + 5 ] = index + 10;
      
               triIndex += 6;
              }
      
              index ++;
            }
         }
      
      
         // - Build Mesh -
         mesh.vertices = verts; 
         mesh.uv = uvs;
         mesh.triangles = tris;
      
         mesh.RecalculateBounds();  
         mesh.RecalculateNormals();
     }
      
      
     void UpdateMesh()
     {
         int index = 0;
      
         for ( int z = 0; z < 9; z ++ )
         {
            for ( int x = 0; x < 9; x ++ )
            {
              verts[ index ] = mapGrid[ x, z ];
      
              index ++;
                 
            }
         }
      
         // assign to mesh
         mesh.vertices = verts;
      
         mesh.RecalculateBounds();
         mesh.RecalculateNormals();


Thanks!

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

Answer by Hoeloe · Oct 11, 2013 at 11:47 PM

Your question makes no sense. Let's just start with a simple fact about Unity:

If it is in the scene, it is necessarily a GameObject.

When you instantiate a mesh, you are not really creating a "mesh", as such, but you are creating a GameObject with a MeshFilter and MeshRenderer attached to it. The reason it's displaying purple is because what you haven't done is attached a material to the renderer, so it has no information as to what the surface should look like. With no information, to prevent a crash, Unity just uses a fallback, which is to draw everything as solid purple. Attach a material to your instantiated MeshRenderer, and then you'll find it works okay.

Comment
Add comment · Show 3 · 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 FusionSausage · Oct 12, 2013 at 08:23 AM 0
Share

Thank you for your answer, but how do I assign a material? I've tried $$anonymous$$aterial mat = Resources.Load("Gravel0031_4_S") as $$anonymous$$aterial; renderer.material = mat; but that doesn't work...

avatar image Hoeloe · Oct 12, 2013 at 09:12 AM 0
Share

What is the resource "Grave10031_4_s"? It sounds to me like that's not a material, and the "as" cast you're doing is then setting it to null.

avatar image FusionSausage · Oct 12, 2013 at 09:50 AM 0
Share

Thank you, I solved it!

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

16 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

Related Questions

GUI Buttons and game objects? 1 Answer

Dynamic mesh positioning with Raycast intersection... 0 Answers

How can I save a gameobject's mesh? 1 Answer

Click on object to activate another object 3 Answers

Building asset bundles and wanting to move game objects from one parent to a newly created parent 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