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
1
Question by Chocolade · Nov 29, 2016 at 10:42 AM · c#scripting problemscript.

Why RaycastHit.triangleIndex return -1 ?

I added a empty GameObject and added to it a Mesh Filter and Mesh Renderer components. And attached to it two scripts.

The first script create a mesh renderer plane.

 using UnityEngine;
 using System.Collections;
 
 public class DragAndDrop : MonoBehaviour
 {
     public float width = 5f;
     public float height = 5f;
 
     private Vector3[] vertices = new Vector3[4];
     private Mesh mesh;
 
     void Start()
     {
         MeshFilter mf = GetComponent<MeshFilter>();
         mesh = new Mesh();
         mf.mesh = mesh;
 
         int[] tri = new int[6];
 
         tri[0] = 0;
         tri[1] = 2;
         tri[2] = 1;
 
         tri[3] = 2;
         tri[4] = 3;
         tri[5] = 1;
 
         // Normals (Only if you want to display the object in the game)
 
         Vector3[] normals = new Vector3[4];
 
         normals[0] = Vector3.forward;
         normals[1] = Vector3.forward;
         normals[2] = Vector3.forward;
         normals[3] = Vector3.forward;
 
         // UVs (How textures are displayed)
 
         Vector2[] UV = new Vector2[4];
 
         UV[0] = new Vector2(0, 0);
         UV[1] = new Vector2(1, 0);
         UV[2] = new Vector2(0, 1);
         UV[3] = new Vector2(1, 1);
 
         // Assgin Arrays!
 
 
         mesh.vertices = vertices;
         mesh.triangles = tri;
         mesh.normals = normals;
         mesh.uv = UV;
     }
 
     void Update()
     {
         vertices = new[] { new Vector3(0, 0, 0), new Vector3(width, 0, 0), new Vector3(0, height, 0), new Vector3(width, height, 0) };
         mesh.vertices = vertices;
     }
 }


The second script should cut holes in the Plane using the mouse button.

 using UnityEngine;
 using System.Collections;
 
 public class CutHole : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     void deleteTri(int index)
     {
         Destroy(gameObject.GetComponent<MeshCollider>());
         Mesh mesh = transform.GetComponent<MeshFilter>().mesh;
 
         int[] oldTriangles = mesh.triangles;
         int[] newTriangles = new int[mesh.triangles.Length - 3];
 
         int i = 0;
         int j = 0;
 
         while(j < mesh.triangles.Length)
         {
             if(j != index*3)
             {
                 newTriangles[i++] = oldTriangles[j++];
                 newTriangles[i++] = oldTriangles[j++];
                 newTriangles[i++] = oldTriangles[j++];
             }
             else
             {
                 j += 3;
             }
         }
         transform.GetComponent<MeshFilter>().mesh.triangles = newTriangles;
         gameObject.AddComponent<MeshCollider>();
     }
 
     // Update is called once per frame
     void Update ()
     {
       if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
 
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if(Physics.Raycast(ray, out hit, 1000.0f))
             {
                 deleteTri(hit.triangleIndex);
             }
         }
     }
 }


I'm using a break point on the line:

 deleteTri(hit.triangleIndex);

And triangleIndex is always -1

And another break point on the line

 newTriangles[i++] = oldTriangles[j++];

This throw the exception:

IndexOutOfRangeException: Array index is out of range. CutHole.deleteTri (Int32 index) (at Assets/MyScripts/CutHole.cs:26) CutHole.Update () (at Assets/MyScripts/CutHole.cs:82)

Screenshot

ss27.jpg (466.9 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

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by CasperK · Nov 29, 2016 at 11:02 AM

Do you also have a Mesh Collider on the GameObject?

From the RaycastHit.triangleIndex documentation:

Triangle index is only valid if the collider that was hit is a MeshCollider.

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 Chocolade · Nov 29, 2016 at 11:20 AM 0
Share

I tried now to add a $$anonymous$$esh Collider to the gameobject but once i click with the mouse on the plane the mesh collider is gone like removed automatic from the inspector. The mesh collider component is removed once i click with the mouse on the plane and i'm getting the same exception and it's still -1.

But when i stop the game the mesh collider is back on the inspector it's gone only when running the game and clicking the plane. Else if the game is not running the mesh collider is in the inspector.

Should i change something in the mesh collider properties ?

avatar image CasperK Chocolade · Nov 29, 2016 at 12:45 PM 0
Share

I've been trying some things and it seems the adding of a mesh on runtime has changed over the years.

I noticed it worked when you'd enable/disable the collider in the inspector.

It's not the nicest fix but you can disable the collider after setting the mesh and enable it again after a milisecond. So from this:

          mesh.vertices = vertices;
          mesh.triangles = tri;
          mesh.normals = normals;
          mesh.uv = UV;
      }

To something like this:

         mesh.vertices = vertices;
         mesh.triangles = tri;
         mesh.normals = normals;
         mesh.uv = UV;
         GetComponent<$$anonymous$$eshCollider>().shared$$anonymous$$esh = mesh;
         GetComponent<$$anonymous$$eshCollider>().enabled = false;
         Invoke("Enable$$anonymous$$eshCollider", .001f);
     }
     void Enable$$anonymous$$eshCollider()
     {
         GetComponent<$$anonymous$$eshCollider>().enabled = true;
     }


avatar image Chocolade CasperK · Nov 29, 2016 at 01:09 PM 0
Share

This is working.

Last question: If i want to create a Plane like in this video ? In fact i tried to do it using this video and trying to understand it.

Video

The Plane there is built from many triangles so when you click on them it's making holes in it. In my Plane script it's creating a Plane built only of two triangles. So when i click on them it's just deleting the Plane.

This is what i mean a Plane with many triangles:

Plane

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

7 People are following this question.

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

Related Questions

How can I animate linerenderer lines over time ? 1 Answer

How do i use a public sealed class to create objects and destroy them ? 0 Answers

How can I call the Load method and/or the ShootingSettings method also only once in the Update ? 1 Answer

How can i check and fire an event when the user look at specific object ? 0 Answers

Why the speed parameter in Animator change the animation speed only for door open but when the door is closing the speed still very fast ? 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