Hi,I meet a problem about OnClick,hope someone can help me.
I use following codes to create thirteen planes ,
public class NewBehaviourScript : MonoBehaviour {
public GameObject PokerCards;
// Use this for initialization
void Start () {
float pos_x = -290.0f, pos_z = -3.0f;
GameObject gameobj_father = GameObject.Find("Panel");
for (int i = 0; i < 13; i++)
{
GameObject temp = Instantiate(PokerCards, new Vector3(0.0f, 0.0f, 0.0f), Quaternion.Euler(90, 180, 0)) as GameObject;
temp.transform.parent = gameobj_father.transform; // 將物件設為父物件的子物件
temp.transform.localPosition = new Vector3(pos_x, -190.0f, pos_z); // 重新設置位置
temp.transform.localScale = new Vector3(10.0f, 0.0f, 10.0f); // 重新設置大小
pos_x += 40.0f;
pos_z -= 0.1f;
detect pokerobj = (detect)temp.GetComponent<detect>();
pokerobj.SetUp(i);
}
}
// Update is called once per frame
void Update () {
}
}
the outcome like following picture. There are thirteen Pokers.
And I write Onclick() in following code to detect mouse event.
using UnityEngine;
using System.Collections;
public class detect : MonoBehaviour {
public int num;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void SetUp(int _num)
{
num = _num;
}
void OnClick()
{
Debug.Log(num + " detect");
}
}
But,when I click each gameObect(Poker) the “Debug.Log(num + " detect”);" return the unexpected num , for example I click the lastest Poker its return was 11 , it should
return 12 ,doesn’t it ?
Could someone give me any hint or keyword to study?