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

[SOLVED, kinda] Instantiating independant gameObjects from the same prefab.

Discussion in 'Scripting' started by Mutajon, Oct 5, 2015.

  1. Mutajon

    Mutajon

    Joined:
    Feb 11, 2015
    Posts:
    4
    Hi,

    Newbie alert!

    I have a prefab with a script to change animation clips upon mouseButtonDown.

    When I instantiate many copies of it using the following code:
    Code (CSharp):
    1. void InitList ()
    2.     {
    3.         for (int x = 1; x < numColumns; x++)
    4.         {
    5.             for ( int y =1; y < numRows; y++)
    6.             {
    7.                 Instantiate (gridUnit, new Vector2(x,y)*spacing, Quaternion.identity);
    8.                
    9.             }
    10.         }
    11.     }
    I get a nice grid of this prefab, however whenever I click one of the instances, all of them change animation clips simultaneously (as if I clicked all of them...).

    How can I avoid this?

    Thanks!
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    post your code where you check Mouse input :)
    but i think you have done something like

    Code (CSharp):
    1. void Update(){
    2.             if (Input.GetKeyDown(KeyCode.Mouse0)) {
    3.                 //do something
    4.             }
    5.         }
    and you are not checking if a specific object was clicked on. and if every object hast this code in where only mouse button down is checked, all will do the same as it happens for you
     
    Kiwasi likes this.
  3. Carvuh

    Carvuh

    Joined:
    Mar 25, 2013
    Posts:
    25
    If you have the same scripts running on an update method across all instances, then they are all going to update their animation clips.

    When you are creating an instance, and are not checking for specific interactions, then their updates will be played across the game world.
     
  4. Mutajon

    Mutajon

    Joined:
    Feb 11, 2015
    Posts:
    4
    Thanks!
    This is indeed the case...
    How can I check if the mouseButtonDown is on the "owner"? i.e. only on the specific instance being clicked?
     
    Last edited: Oct 5, 2015
  5. Mutajon

    Mutajon

    Joined:
    Feb 11, 2015
    Posts:
    4
    This is the current code (for some reason it won't let me edit the previous message with it):
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GridUnit : MonoBehaviour {
    5.  
    6.     private tk2dSpriteAnimator            gridUnitAnimator;
    7.     private int                            unitState = 2;  
    8.  
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.         gridUnitAnimator = GetComponent<tk2dSpriteAnimator>();
    13.     }
    14.  
    15.     void Update ()
    16.     {
    17.     if (Input.GetMouseButtonDown(0))
    18.         {
    19.             unitState --;
    20.         }
    21.  
    22.         switch (unitState)
    23.         {
    24.         case -1:
    25.             unitState = 2;
    26.             gridUnitAnimator.Play("Empty");
    27.             break;
    28.         case 0:
    29.             gridUnitAnimator.Play("Green");
    30.             break;
    31.         case 1:
    32.             gridUnitAnimator.Play("Red");
    33.             break;
    34.         default:
    35.             gridUnitAnimator.Play("Empty");
    36.             break;
    37.  
    38.         }
    39.     }
    40.  
    41.  
    42. }
     
  6. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,887
    There are a few ways to do it.

    One is to use the IPointerClickHandler interface like so:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class ClickableScript : MonoBehaviour, IPointerClickHandler {
    6.  
    7.     public void OnPointerClick(PointerEventData eventData)
    8.     {
    9.         Debug.Log("Clicked " + gameObject.name);
    10.     }
    11.  
    12. }
    Or use some type of HoveredObjectManager script that uses a raycast from the camera to store the currently hovered object. Then check the hovered object to see if it has the component you need. This script should only exist once in the scene.

    Or do it all in one script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class InputScript : MonoBehaviour {
    5.  
    6.     public float rayDistance = 1000f;
    7.     public float LayerMask rayLayerMask;
    8.  
    9.     void Update
    10.     {
    11.         if (Input.GetMouseButtonDown(0))
    12.         {
    13.             // Get hit object. If null, stop
    14.             GameObject hitObj = GetHitObject();
    15.             if (hitObj == null) return;
    16.  
    17.             // Check hit object for component. If not found, stop
    18.             ChangeClipScript clipScript = hitObj.GetComponent<ChangeClipScript>();
    19.             if (clipScript == null) return;
    20.  
    21.             clipScript.Change();
    22.         }
    23.     }
    24.  
    25.     GameObject GetHitObject()
    26.     {
    27.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    28.  
    29.         RaycastHit hit;
    30.  
    31.         if (Physics.Raycast(ray, out hit, rayDistance, rayLayerMask))
    32.         {
    33.             return hit.collider.gameObject;
    34.         }
    35.         else
    36.         {
    37.             return null;
    38.         }
    39.     }
    40.  
    41. }
     
    Last edited: Oct 5, 2015
  7. Mutajon

    Mutajon

    Joined:
    Feb 11, 2015
    Posts:
    4
    As a beginner, this all goes over my head, but I will try to go into it further and search what I do not understand before asking any more questions.
    Thanks, greatly appreciate it!