Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Same script on multiple object

Discussion in 'Scripting' started by jacoposciampi, May 31, 2018.

  1. jacoposciampi

    jacoposciampi

    Joined:
    May 30, 2018
    Posts:
    1
    Hello everyone. I don't know if it is a common question but I'm trying to do something in unity 3d.

    I made a script that simply destroy an object after 5 clicks. Here's the snippet :

    Code (CSharp):
    1. public class tree : MonoBehaviour {
    2.     public int life = 5;
    3.     CapsuleCollider bc;
    4.  
    5.     void Update()
    6.     {
    7.         if (Input.GetMouseButtonDown(0))
    8.         {
    9.             RaycastHit hit;
    10.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    11.             if (Physics.Raycast(ray, out hit))
    12.             {
    13.                 bc = hit.collider as CapsuleCollider;
    14.                 if (bc != null)
    15.                 {
    16.                     life--;
    17.  
    18.                     if (life <= 0)
    19.                     {
    20.                         var ob = this.gameObject;
    21.                         Rigidbody rb = ob.AddComponent<Rigidbody>();
    22.                         rb.isKinematic = false;
    23.                         rb.useGravity = true;
    24.                         rb.AddForce(Vector3.forward, ForceMode.Impulse);
    25.                         Destroy(bc.gameObject);
    26.                     }
    27.  
    28.                 }
    29.             }
    30.         }
    31.     }
    32. }
    What Im trying to do is to have multiple object related to this script.

    What I have in my project : One tree that gets desotryed after 5 clicks. Perfect. I have now added a second same tree, and linked the same script. But when I hit one tree, both of them gets destroyed. How to handle that? I mean, I tought that even if the script is the same it will be refered to two istance.
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    First off, you are correct that you can add the same script to multiple objects and that they will all have their own instance of that script.

    There are, however, a few issues with your script:
    1. You don't want to be doing the ray cast there. If you had one thousand objects, you would be firing the same ray one thousand times. That is not necessary.
    2. The line
      if (bc != null)
      simply says "if the ray cast hit anything that contains a capsule collider". That means, the fact that the ray hit another object will be detected in this script and will destroy this object as well.
    So you want to separate out the part that detects if an object has been clicked on and then tell that one object to reduce its life count by one.
     
  3. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Good way to simplify this script is to use the built in
    OnMouseDown
    event:

    Code (CSharp):
    1. public class tree : MonoBehaviour {
    2.     public int life = 5;
    3.  
    4.     void OnMouseDown () {
    5.         life -= 1;
    6.         if( life <= 0 ) {
    7.             var ob = gameObject;
    8.             Rigidbody rb = ob.AddComponent<Rigidbody>();
    9.             rb.isKinematic = false;
    10.             rb.useGravity = true;
    11.             rb.AddForce( Vector3.forward, ForceMode.Impulse );
    12.             Destroy( ob );
    13.         }
    14.     }
    15. }
    Though you're destroying the GameObject immediately so you're not really giving the rigidbody much time to take effect...
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Agree that's better. The reason I didn't give that option is simply because I forgot about it. :eek:

    A valid point, the Destroy could be changed to something like Destroy( ob, 1f ) to give some time. I guess it depends on the OP's objective. :)