Hey guys,
So here’s my issue, I want to instantiate multiple prefabs with different components in the attached script and set the prefab child button Onclick to a function located in the script.
GameObject prefab;
public GameObject parentGameObject;
void LoadPrefab() {
prefab = (GameObject)Instantiate(Resources.Load("MyPrefab"));
prefab.transform.SetParent(parentGameObject.transform);
prefab.transform.localScale = new Vector3(1,1,1);
prefab.AddComponent<MyScript>();
prefab.AddComponent<MyScript>();
prefab.GetComponent<MyScript>().DataInt = Data;
prefab.GetComponent<MyScript>().DataVector = DataVector.point;
prefab.transform.Find ("btn_goTo").GetComponent<Button> ().onClick.AddListener (delegate {prefab.GetComponent<MyScript>().FunctionInMyScript();}); }
The aim is : when the user click on the button, my camera is moving to the DataVector position.
Now each prefab seems to have the same script attached… How can I fixed this ?
Thanks in advance !