- Home /
Question by
saswilson · Aug 19, 2013 at 06:52 PM ·
prefabgetcomponent
Accessing a component in a Prefab
How can I access a component in a prefab? I have this script on a GO
public Transform prefab_Shadow;
public void SpawnShadow () {
Transform newShadow = Instantiate (prefab_Shadow,transform.position,transform.rotation) as Transform;
newShadow.transform.parent = gameObject.transform.parent;
So, I need to access the projector on this prefab (prefab_Shadow) Thanks for any help!
Comment
Best Answer
Answer by whebert · Aug 19, 2013 at 08:46 PM
Capture your instantiated reference as a GameObject, then use GetComponent, like so:
public GameObject prefab_Shadow;
GameObject newShadow = Instantiate (prefab_Shadow,transform.position,transform.rotation) as GameObject;
Projector projector = newShadow.GetComponent<Projector>();
Looks good to me, thanks a lot! I'd buy you a beer if I could!