Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Hamlet-Archer · Nov 23, 2012 at 07:27 AM · instantiatestaticextension

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shrandis · Nov 23, 2012 at 07:45 AM 0
Share

Please don't forget to click thumbs up and checkmark icons near the answer to close the question if the answer is correct.

avatar image Hamlet-Archer · Nov 23, 2012 at 08:56 AM 0
Share

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.

avatar image Shrandis · Nov 23, 2012 at 09:00 AM 0
Share

I think there is a karma requirement to vote :(

avatar image Hamlet-Archer · Nov 23, 2012 at 09:29 AM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to spawn on random postion but non on static positions 0 Answers

How do I instantiate a Prefab using a MenuItem? 1 Answer

Checking if object intersects? 1 Answer

instantiate spawning meany objects in multiplayer 0 Answers

How to Instantiate objects while in editor? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges