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

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:

void InitList ()
    {
        for (int x = 1; x < numColumns; x++)
        {
            for ( int y =1; y < numRows; y++)
            {
                Instantiate (gridUnit, new Vector2(x,y)*spacing, Quaternion.identity);
               
            }
        }
    }

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!

post your code where you check Mouse input :slight_smile:
but i think you have done something like

void Update(){
            if (Input.GetKeyDown(KeyCode.Mouse0)) {
                //do something
            }
        }

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

1 Like

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.

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?

This is the current code (for some reason it won’t let me edit the previous message with it):

using UnityEngine;
using System.Collections;

public class GridUnit : MonoBehaviour {

    private tk2dSpriteAnimator            gridUnitAnimator;
    private int                            unitState = 2;   

    // Use this for initialization
    void Start ()
    {
        gridUnitAnimator = GetComponent<tk2dSpriteAnimator>();
    }

    void Update ()
    {
    if (Input.GetMouseButtonDown(0))
        {
            unitState --;
        }

        switch (unitState)
        {
        case -1:
            unitState = 2;
            gridUnitAnimator.Play("Empty");
            break;
        case 0:
            gridUnitAnimator.Play("Green");
            break;
        case 1:
            gridUnitAnimator.Play("Red");
            break;
        default:
            gridUnitAnimator.Play("Empty");
            break;

        }
    }


}

There are a few ways to do it.

One is to use the IPointerClickHandler interface like so:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class ClickableScript : MonoBehaviour, IPointerClickHandler {

    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("Clicked " + gameObject.name);
    }

}

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:

using UnityEngine;
using System.Collections;

public class InputScript : MonoBehaviour {

    public float rayDistance = 1000f;
    public float LayerMask rayLayerMask;

    void Update
    {
        if (Input.GetMouseButtonDown(0))
        {
            // Get hit object. If null, stop
            GameObject hitObj = GetHitObject();
            if (hitObj == null) return;

            // Check hit object for component. If not found, stop
            ChangeClipScript clipScript = hitObj.GetComponent<ChangeClipScript>();
            if (clipScript == null) return;

            clipScript.Change();
        }
    }

    GameObject GetHitObject()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, rayDistance, rayLayerMask))
        {
            return hit.collider.gameObject;
        }
        else
        {
            return null;
        }
    }

}

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!