- Home /
Question by
Beloudest · Aug 18, 2016 at 02:05 PM ·
instantiateprefabs
Best Way To Reference Instantiated Prefabs with Each Other.
Hi I did some research on Instantiating multiple Prefabs that reference each other and started implementing referencing the correct prefab via find methods like so:
var children = cockpit.GetComponentsInChildren<Transform> ();
foreach (var child in children){
if (child.name == "GUICanvas") {
speedGUI = child.transform.Find("Speed").gameObject;
rpmGUI = child.transform.Find("RPM").gameObject;
yawGUI = child.transform.Find("YawSlider").gameObject;
pitchGUI = child.transform.Find("PitchSlider").gameObject;
rollGUI = child.transform.Find("RollSlider").gameObject;
throttleGUI = child.transform.Find("ThrottleSlider").gameObject;
thrustGUI = child.transform.Find("ThrustSlider").gameObject;
trimGUI = child.transform.Find("TrimSlider").gameObject;
primaryWeaponGUI = child.transform.Find("PrimaryWeapon").gameObject;
secondaryWeaponGUI = child.transform.Find("SecondaryWeapon").gameObject;
}
if (child.name == "Altitude") {
altitudeGUI = child.gameObject;
}
if (child.name == "RingOfFire") {
fireDeath = child.gameObject;
}
if (child.name == "CampFire") {
fireSmall = child.gameObject;
}
if (child.name == "Native Cam") {
NativeCam = child.gameObject;
deathAnimation = child.gameObject.GetComponent<Animation> ();
}
if (child.name == "Controller (Left)") {
Debug.Log ("Hand found");
LeftHand = child.gameObject;
}
}
It feels rather ugly, is there another way of approaching this? Essentially there are 2 prefabs cross-referencing each other. When I Instantiate the prefabs many of the references are missing as the instantiated object will look for the gameobjects referenced when the prefab was created which does not exist.
Hope it makes sense?
Comment
As it is link prefab to prefab can't you simply use public gameobjects linked in the inspector. @Beloudest (Ignore if this is no help)
Your answer
![](https://koobas.hobune.stream/wayback/20220612091048im_/https://answers.unity.com/themes/thub/images/avi.jpg)