Hello, I’m new to unity and I’m in the middle of building a game where prefabs are spawned and I need to change a bool on whatever specific prefab I click on and press a certain dialog button on. The dialog will be children in the prefab so that when the buttons become active, they should be able to detect which prefab their “parent” is and change that specific prefab. I was wondering if anyone could give me advice on how to do this. Thank you!
You should have a script on the prefab so it can just do something with the OnClick on the button. This way it just references itself and can find it’s own parent.
You can assign whatever you wish when you spawn the prefab.
For example
public class Spawner : MonoBehaviour {
public GameObject MyPrefab;
public void SpawnPrefab()
{
var prefab = Instantiate(MyPrefab);
var myScript = prefab.GetComponent<MyScript>();
myScript.Parent = transform;
}
}
with myscript looking like this
using UnityEngine;
public class MyScript : MonoBehaviour
{
public Transform Parent;
}