• 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 dkfister · Jan 15, 2014 at 03:19 AM · c#meshvector3arraystriangles

Failed setting triangles in my mesh

So, I'm trying to create an automated "Face Creator" for my Mesh, so I can just call a function, to create a face in a specific position, and height/width.

But I'm getting the error: Failed setting triangles. The number of supplied triangle indices must be a multiple of 3.

Which is weird, because I am actually using 6 ints, for the 2 triangles that a face have.

This is my code;

 using UnityEngine;
 using System.Collections;
 
 public class RoomCreator : MonoBehaviour {

 private Vector3[] Mesh_Vertices;
 private Vector2[] Mesh_Uvs;
 private int[] Mesh_Triangles;

 private MeshFilter meshFilter;

 void Start() {
     firstFace = true;

     Mesh_Vertices = new Vector3[100];
     Mesh_Uvs = new Vector2[100];
     Mesh_Triangles = new int[100];

     meshFilter = GetComponent<MeshFilter>();

     CreateRoom();
 }

 private void CreateRoom() {
     meshFilter.mesh = CreateMesh();
 }

 private Mesh CreateMesh() {
     Mesh m = new Mesh();
     m.name = gameObject.name + "_Mesh";

     CreateFace(new Vector3(0,0,0), 1,1);

     m.vertices = Mesh_Vertices;
     m.triangles = Mesh_Triangles;
     m.uv = Mesh_Uvs;
     m.RecalculateNormals();

     return m;
 }

 private void CreateFace(Vector3 position,int width, int height) {
     Mesh_Vertices[0] = new Vector3(position.x, position.y, 0);
     Mesh_Vertices[1] = new Vector3(position.x + width, position.y, 0);
     Mesh_Vertices[2] = new Vector3(position.x + width, position.y + height, 0);
     Mesh_Vertices[3] = new Vector3(position.x, position.y + height, 0);

     Mesh_Triangles[0] = 0;
     Mesh_Triangles[1] = 1;
     Mesh_Triangles[2] = 2;

     Mesh_Triangles[3] = 0;
     Mesh_Triangles[4] = 2;
     Mesh_Triangles[5] = 3;

     Mesh_Uvs[0] = new Vector2(0,0);
     Mesh_Uvs[1] = new Vector2(0,1);
     Mesh_Uvs[2] = new Vector2(1,1);
     Mesh_Uvs[3] = new Vector2(1,0);
 }

}

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

Answer by Eric5h5 · Jan 15, 2014 at 03:21 AM

You have Mesh_Triangles = new int[100];. 100 is not a multiple of 3. If you're using 6 triangle indices then the triangles array should be of size 6.

Comment
Add comment · Show 5 · 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 dkfister · Jan 15, 2014 at 03:24 AM 0
Share

I should really have realized that. I don't know why I didn't. Either way, it fixed it! $$anonymous$$uch appreciated mate.

avatar image dkfister · Jan 15, 2014 at 03:30 AM 0
Share

Don't know if you are still there, but the problem resumes, now the Array index is out of range.

avatar image dkfister · Jan 15, 2014 at 03:31 AM 0
Share

IndexOutOfRangeException: Array index is out of range. RoomCreator.CreateFace (Vector3 position, Int32 width, Int32 height) (at Assets/Scripts/BuildingToolsDeveloper/RoomCreator.cs:42) RoomCreator.Create$$anonymous$$esh () (at Assets/Scripts/BuildingToolsDeveloper/RoomCreator.cs:30) RoomCreator.CreateRoom () (at Assets/Scripts/BuildingToolsDeveloper/RoomCreator.cs:23) RoomCreator.Start () (at Assets/Scripts/BuildingToolsDeveloper/RoomCreator.cs:19)

avatar image Eric5h5 · Jan 15, 2014 at 03:47 AM 0
Share

You're referring to an element in an array, where the array isn't big enough. The code you have here is presumably not the code you're using now, since the line numbers don't match up, so I can't say anything more.

avatar image dkfister · Jan 15, 2014 at 03:51 AM 0
Share

Created a new question, that have this in account.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

19 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

Related Questions

Array index is out of range 2 Answers

Best way to optimize mesh updating through code? 1 Answer

Modify mesh problems 1 Answer

Setting mesh.vertices to a 3-dimensional array 1 Answer

Get forward direction of face hitted by Raycast. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges