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 /
  • Help Room /
This question was closed Feb 01, 2020 at 11:45 AM by liderrgaming for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by liderrgaming · Feb 01, 2020 at 12:31 AM · scripting problemmesh

Mesh not rendering correctly

My randomly generated mesh is not rendering for some reason Below is the script and a picture of the problemalt text

[2]:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]

public class MeshGenerator : MonoBehaviour {

 Mesh mesh;
 Vector3[] verts;
 int[] triangles;

 public int xSize = 20;
 public int zSize = 20;

 private void Start()
 {
     mesh = new Mesh();
     GetComponent<MeshFilter>();

     CreateShape();
     UpdateMesh();
 }

 void CreateShape()
 {
     verts = new Vector3[(xSize + 1) * (zSize + 1)];

     int i = 0;
     for (int z = 0; z <= zSize; z++)
     {
         for (int x = 0; x <= xSize; x++)
         {
             verts[i] = new Vector3(x, 0, z);
             i++;
         }
     }

     triangles = new int[xSize * zSize * 6];
     int vert = 0;
     int tris = 0;
     for (int z = 0; z < zSize; z++) 
     {
         for (int x = 0; x < xSize; x++)
         {
             triangles[tris + 0] = vert + 0;
             triangles[tris + 1] = vert + xSize + 1;
             triangles[tris + 2] = vert + 1;
             triangles[tris + 3] = vert + 1;
             triangles[tris + 4] = vert + xSize + 1;
             triangles[tris + 5] = vert + xSize + 2;
             vert++;
             tris += 6;
         }
         vert++;
     }
    

 }

 void UpdateMesh()
 {
     mesh.Clear();

     mesh.vertices = verts;
     mesh.triangles = triangles;

     mesh.RecalculateNormals();

 }

 private void OnDrawGizmos()
 {
     if(verts == null)
     {
         return;
     }

     for (int i = 0; i < verts.Length; i++)
     {
         Gizmos.DrawSphere(verts[i], .1f);
     }
 }

}

capture.png (139.3 kB)
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

  • Sort: 
avatar image
0

Answer by Bunny83 · Feb 01, 2020 at 12:41 AM

Uhm, you never assigned your mesh to your MeshFilter component. You call GetComponent<MeshFilter>() in Start wtithout doing anything with it. You probably wanted to do

 GetComponent<MeshFilter>().sharedMesh = mesh;
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 liderrgaming · Feb 01, 2020 at 11:45 AM 0
Share

Turns out late night coding you are bound to get mistakes. Thanks Bunny83 it worked perfectly

avatar image tkati · Sep 02, 2021 at 03:01 AM 0
Share

I had a similar issue on a very similar setup but I was assigning the mesh.

For whatever reason had to remove the script on the game object then reattach the script component. The inspector then loaded the "Required" components.

avatar image Bunny83 tkati · Sep 02, 2021 at 02:10 PM 0
Share

The reason is quite simple. The RequiredComponents attribute is a pure editor feature that only takes action in the moment when you attach the script to a gameobject in the editor. It does nothing if you add this attribute when the script is already attached. It also does nothing when you add your component through scripting by AddComponent. This is just a convenient WYSIWYG feature. If you were missing a MeshFilter and a MeshRenderer component, you would have to attach them manually. Though your case is very different from the questio the OP as posted, since we can clearly see that the object does have all those components attached.

Follow this Question

Answers Answers and Comments

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

Destroy shattered objects after being smashed. 0 Answers

Open canvas clicking on a mesh? 1 Answer

Changing a mesh with C# and Resource.Load() 0 Answers

Using Zed Camera Mesh Maker VR 0 Answers

Mesh missing in prefab 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