- Home /
how to instantiate in an extension?
i am trying to instantiate a prefab inside an extension:
public static class ChickenExtension {
public static GameObject eggPrefab;
public static void LayEgg(this Chicken chicken) {
Instantiate(eggPrefab);
}
}
i get the error:
error CS0103: The name `Instantiate' does not exist in the current context
i believe the reason has to do with the ChickenExtension class not deriving from mono, but since an extension requires a static class, i cannot derive it from mono either. how should i go about doing this?
Answer by Shrandis · Nov 23, 2012 at 07:43 AM
Instantiate method is inherited from UnityEngine.Object. Because your class ChickenExtension is not derived from UnityEngine.Object, you need to add:
using UnityEngine;
and call the method like this:
GameObject.Instantiate(eggPrefab);
Please don't forget to click thumbs up and checkmark icons near the answer to close the question if the answer is correct.
thanks so much! that solves my problem. how come i can't vote up the answer? i get a message saying that i don't have permission.
ohh... i'll work hard on my karma and come back to vote up your answer once i meet the requirement. i am afraid my heartfelt gratitude is all i can offer meanwhile. thank you again. :-)
Your answer
![](https://koobas.hobune.stream/wayback/20220613084355im_/https://answers.unity.com/themes/thub/images/avi.jpg)