Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Duplicated game objects and using the same script for them

Discussion in 'Scripting' started by Baxan, Dec 12, 2015.

  1. Baxan

    Baxan

    Joined:
    Dec 12, 2015
    Posts:
    2
    Hi,

    I'm having trouble when I try to use same script on duplicate game objects.

    Here is my script :

    Code (CSharp):
    1. public class SelectObjectScript : MonoBehaviour
    2. {
    3.     public int Id;
    4.  
    5.     void Start()
    6.     {
    7.      
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         if (Input.GetMouseButtonDown(0))
    13.         {
    14.             Debug.Log("My Id is " + this.Id.ToString());
    15.         }
    16.     }
    17. }
    When I click one of the objects, I expect only 1 log line in console.
    "My Id is 1" etc.

    But console messages are like;
    "My Id is 1"
    "My Id is 2"
    "My Id is 3"
    "My Id is 4"

    It looks like the script is running for each object...

    Can someone help me please?

    I'm really noob :)
     
  2. Sose

    Sose

    Joined:
    Dec 10, 2015
    Posts:
    27
    I don't understand what sort of behaviour you expect.. Scripts are components, you add them to GameObjects. Every GameObject has their instance of every component they use (this is true for Unity's native components like Rigidbody, Animation, MeshRenderer, Transform and for your own scripts)

    If you only want to have the script on one GameObject, simply only add it to one GameObject.

    Sorry but I don't understand your issue at all

    Edit: I realized you might be thinking that GetMouseButtonDown(button) would only return true if you click on the object. It will simply return true on the frame when the mouse button is pressed, no matter where the cursor is
     
  3. 3zzerland

    3zzerland

    Joined:
    Oct 31, 2014
    Posts:
    42
    Your script says that when mouse button 0 is clicked, respond. It doesn't say "only respond if I am the object you clicked on."

    If you only want 1 item to identify itself you need to add in a collision check to verify which item was clicked on.
    I recommend doing a search for this problem. You can find almost anything that way :)

    Let me know if you still have trouble after that.
     
    Baxan likes this.
  4. Sose

    Sose

    Joined:
    Dec 10, 2015
    Posts:
    27
    Baxan likes this.
  5. Baxan

    Baxan

    Joined:
    Dec 12, 2015
    Posts:
    2
    Thanks guys.

    Adding a box collider and OnMouseDown event solved the problem.

    Cheers ;)