GameObject.Find works once only?

Hi,

Quick intro, I’m trying to find my prefabs subcomponent called ‘Model’ and then instantiate another prefab once found with it’s rotation changed to that of the model, for me this only happens the once, not sure why…

model = GameObject.Find ("Model"); in Start () which is fine of course, I made a reference to it with a private gameobject

if (model)
								Instantiate (damagePrefab, model.transform.position, model.transform.rotation);

which is fine, then boom it’s gone, one hit wonder, if I don’t have the boolean check it tells me the object has been destroyed and your still trying to access it… so basically my prefab has this subcomponent named Model which has the transform I’m interested in and I thought doing the GameObject.Find method might be one way of doing it, perhaps I need to reference the ‘model’ transform first or cache it… hmmm please advise, I’m sure it simple… but I’m hungry now, need tune sandwich, cheers

private Transform model;
model = GetComponentInChildren<Transform> ();
if (model) Instantiate (damagePrefab, model.transform.position, model.transform.localRotation);

All hail the tuna!

damn still getting the original transform by the looks of it… oh well

You could assign it in the editor using a public Transform

1 Like

yeah, I plumbed for that in the end… I just wanted to make my first prefab without having to manually assign that model transform every-time, still it works… I did think of it then tried the alternative route but this’ll do for now, cheers

Result HERE