I have a childObject that i want to have a event listenner when onMoueUp is executed, i made the configuration bellow but the desired behaviour is not achieved.
On a GameObject i have a reference to a preFab GameObject.
This Prefab GO has a collider that isTrigger and the code for this preFab is the following
using UnityEngine;
using System.Collections;
public class Clickable : MonoBehaviour {
public delegate void ClickAction();
public event ClickAction onClicked;
void OnMouseDown() {
if(onClicked != null){
Debug.Log(" HAS CLICK");
this.onClicked();
}
else{
Debug.Log("NO CLICKLISTENER");
}
}
}
On the parent i have the following:
using UnityEngine;
using System.Collections;
public class VehicleRenderer : MonoBehaviour {
public float posX,posY,posZ;
public GameObject vehicle;
public string vehicleName;
// Use this for initialization
void Start () {
Quaternion spawnRotation = Quaternion.identity;
Vector3 spawnPosition = new Vector3 (posX,posY, posZ);
//Create the prefab
Instantiate (vehicle, spawnPosition, spawnRotation);
Clickable clickable=vehicle.GetComponent<Clickable>();
clickable.onClicked+=vehicleClicked;
}
// Update is called once per frame
void Update () {
}
void vehicleClicked(){
Debug.Log(vehicleName + " Clicked");
}
}
When i click NO CLICKLISTENER is always logged, and the VehicleRenderer click is never invoked