• 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 /
avatar image
0
Question by Chocolade · Nov 30, 2016 at 11:14 PM · c#scripting problemscript.

How can i make sure the script will work only on the attached object ?

In my script when i click the mouse it will cut hole in the meshed object. The problem is that i have in the Hierarchy like 4 objects in the same name Plane and they are all realy Planes.

So if i click on other Plane object it will effect the plane the script is attached to also. But i want that if i click other Planes object it will not do anything. Only if i click on the Plane the script is attached to !

This is a screenshot showing the Hierarchy i have there 4 Planes and one Plane (1) But the script is attached only to the Plane after the GameObject (1)

Screenshot

And this is the script. I can't figure out how to make it to work only on the Plane object it's attached to and not others. So if i will click other Planes it will do nothing ! Only if i click on the Plane it's attached to.

 using UnityEngine;
 using System.Collections;
 
 public class cutHole : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
 
     void deleteSquare(int index1, int index2)
     {
         Destroy(this.gameObject.GetComponent<MeshCollider>());
         Mesh mesh = transform.GetComponent<MeshFilter>().mesh;
         int[] oldTriangles = mesh.triangles;
         int[] newTriangles = new int[mesh.triangles.Length-6];
 
         int i = 0;
         int j = 0;
         while (j < mesh.triangles.Length)
         {
             if(j != index1*3 && j != index2*3)
             {
                 newTriangles[i++] = oldTriangles[j++];
                 newTriangles[i++] = oldTriangles[j++];
                 newTriangles[i++] = oldTriangles[j++];
             }
             else
             {
                 
                 j += 3;
             }
         }
         transform.GetComponent<MeshFilter>().mesh.triangles = newTriangles;
         this.gameObject.AddComponent<MeshCollider>();
 
     }
 
     int findVertex(Vector3 v)
     {
         Vector3[] vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
         for(int i = 0; i < vertices.Length; i++)
         {
             if(vertices[i] == v)
                 return i;
         }
         return -1;
     }
 
     int findTriangle(Vector3 v1, Vector3 v2, int notTriIndex)
     {
         int[] triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
         Vector3[] vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
         int i = 0;
         int j = 0;
         int found = 0;
         while (j < triangles.Length)
         {
             if(j/3 != notTriIndex)
             {
                 if(vertices[triangles[j]] == v1 && (vertices[triangles[j+1]] == v2 || vertices[triangles[j+2]] == v2))
                     return j/3;
                 else if(vertices[triangles[j]] == v2 && (vertices[triangles[j+1]] == v1 || vertices[triangles[j+2]] == v1))
                     return j/3;
                 else if(vertices[triangles[j+1]] == v2 && (vertices[triangles[j]] == v1 || vertices[triangles[j+2]] == v1))
                     return j/3;
                 else if(vertices[triangles[j+1]] == v1 && (vertices[triangles[j]] == v2 || vertices[triangles[j+2]] == v2))
                     return j/3;
             }
 
             j+=3;
         }
 
         return -1;
     }
 
     void deleteTri(int index)
     {
         Destroy(this.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;
         this.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))
             {
                 int[] triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
                 if (hit.triangleIndex != -1 && (hit.triangleIndex * 3) < triangles.Length)
                 {
                     int hitTri = hit.triangleIndex;
 
                     //get neighbour
                     
                     Vector3[] vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
                     Vector3 p0 = vertices[triangles[hitTri * 3 + 0]];
                     Vector3 p1 = vertices[triangles[hitTri * 3 + 1]];
                     Vector3 p2 = vertices[triangles[hitTri * 3 + 2]];
 
                     float edge1 = Vector3.Distance(p0, p1);
                     float edge2 = Vector3.Distance(p0, p2);
                     float edge3 = Vector3.Distance(p1, p2);
 
                     Vector3 shared1;
                     Vector3 shared2;
                     if (edge1 > edge2 && edge1 > edge3)
                     {
                         shared1 = p0;
                         shared2 = p1;
                     }
                     else if (edge2 > edge1 && edge2 > edge3)
                     {
                         shared1 = p0;
                         shared2 = p2;
                     }
                     else
                     {
                         shared1 = p1;
                         shared2 = p2;
                     }
 
                     int v1 = findVertex(shared1);
                     int v2 = findVertex(shared2);
 
                     deleteTri(hit.triangleIndex);
                 }
             }
         }
     }
 }
 
ss29.jpg (503.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
0
Best Answer

Answer by tkamruzzaman · Dec 01, 2016 at 02:44 AM

Instead of using Update you should use OnMouseDown();

 using UnityEngine;
 
 public class cutHole : MonoBehaviour {
 
     int findVertex(Vector3 v) {
         Vector3[] vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
         for (int i = 0; i < vertices.Length; i++) {
             if (vertices[i] == v)
                 return i;
         }
         return -1;
     }
 
     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>();
     }
 
     void OnMouseDown() {
         if (Input.GetMouseButtonDown(0)) {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit, 1000.0f)) {
                 int[] triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
                 if (hit.triangleIndex != -1 && (hit.triangleIndex * 3) < triangles.Length) {
                     int hitTri = hit.triangleIndex;
 
                     //get neighbour
                     Vector3[] vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
                     Vector3 p0 = vertices[triangles[hitTri * 3 + 0]];
                     Vector3 p1 = vertices[triangles[hitTri * 3 + 1]];
                     Vector3 p2 = vertices[triangles[hitTri * 3 + 2]];
 
                     float edge1 = Vector3.Distance(p0, p1);
                     float edge2 = Vector3.Distance(p0, p2);
                     float edge3 = Vector3.Distance(p1, p2);
 
                     Vector3 shared1;
                     Vector3 shared2;
                     if (edge1 > edge2 && edge1 > edge3) {
                         shared1 = p0;
                         shared2 = p1;
                     }
                     else if (edge2 > edge1 && edge2 > edge3) {
                         shared1 = p0;
                         shared2 = p2;
                     }
                     else {
                         shared1 = p1;
                         shared2 = p2;
                     }
 
                     int v1 = findVertex(shared1);
                     int v2 = findVertex(shared2);
 
                     deleteTri(hit.triangleIndex);
                 }
             }
         }
     }
 }

Comment
Add comment · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

I create material with script but it does not render right 0 Answers

Creating Splines from empties in script 0 Answers

How can i rotate all the child objects together at the same time ? 1 Answer

how to ask if something is not true for an amount of time 2 Answers

How can i check if the alpha color or the material color is transparent ? 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