• 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
Question by Captain_Dando · Oct 08, 2013 at 01:09 PM · prefabcoroutinestatic

How can I instantiate a prefab from a static function?

Hi everyone, I have a script w$$anonymous$$ch instantiates a prefab. as it is called often, and on many scripts, I would prefer to have it as a static function rather than create a reference on almost every script. the problem is, I can't declare a prefab and then have it called in the static function. I get the error "An object reference is required to access non-static member 'example'

Here is a snippet that replicates the problem;

 public class ExampleForUnityAnswers : MonoBehaviour {
      public GameObject Prefab;
      public static IEnumerator Example(){
           GameObject GO = new GameObject("Prefab");
                               GO = Instantiate(Prefab, new Vector3(0,0,0), Quaternion.identity) as GameObject;
                               yield return null;
      }   
 }
Comment
klesun

People who like this

1 Show 7
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 vexe · Oct 08, 2013 at 01:12 PM 1
Share

The prefab as to be static as well in order for you to use it in your static function.

avatar image vexe · Oct 08, 2013 at 01:21 PM 1
Share

Yes, that's the static price you have to pay. Assign it manually via code somewhere. Statics are generally evil.

I would prefer to have it as a static function rather than create a reference on almost every script

This sounds to me like you're lazy. Are you sure you must use statics? - See this, jump to good coding habits section and read when should and shouldn't you use something static.

If it's a must, and you're dealing with say, a GameManager, or AtlasManager, etc. Then at least use a Singleton.

avatar image vexe · Oct 08, 2013 at 01:28 PM 1
Share

You mean instantiate a prefab, and have a variable reference it? How about doing it in Awake?

avatar image vexe · Oct 08, 2013 at 01:37 PM 1
Share

See this (Resource.Load), and this.

avatar image Fattie · Oct 08, 2013 at 02:33 PM 1
Share

"I've never really used static functions before, but I felt that maybe my objects had too many references and that it was starting to look untidy." indeed if you use the grid "system" below to be grandiose, you will only have that one static (unless you have some others that do say math or who knows what), and then you will be able to have no statics whatsoever, other than that. so it's a good intro when you're just learning.

mis-use of statics by beginners drives me frickin nuts. you will often see me lying on here "Oh, statics have been removed from the new Intel chips, you must not use them now." to help beginners :)

as Vexe intelligently alerts us, static prefabs are the like are a nightmare and not to be involved with.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Fattie · Oct 08, 2013 at 02:21 PM

Latest version is here ...

http://answers.unity3d.com/answers/1128034/view.html

PS here's the latest version of the .Grid system

http://answers.unity3d.com/answers/663681/view.html

It couldn't be easier. Don't forget that the popular "grid" system is strictly just some syntactic candy, to save you typing one line of code - it's not a big deal in itself.

Comment
Captain_Dando
Jamora
vexe
fafase
TechSupportIncoming1
Huacanacha
rakkarage

People who like this

7 Show 8 · 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 Captain_Dando · Oct 08, 2013 at 02:32 PM 0
Share

Thanks Fattie, I'll give it a shot

avatar image Jamora · Oct 08, 2013 at 02:36 PM 0
Share

As to how you can easily start your game from that special "tiny scene", this question has a few ideas.

avatar image vexe · Oct 08, 2013 at 02:41 PM 0
Share
  • just by reading the comments in the answer lol makes me feel that the whole world is safe XD - Man @Fattie I wish you'd be a president or something, that way we would have less evil, and live KISSly ever after :D

avatar image Fattie · Oct 08, 2013 at 02:47 PM 0
Share

LOL damn right, Jamora! :)

Dude the whole world IS safe - you never have to worry!! I could not be the president though man as I am drunk just now :/

avatar image vexe · Oct 08, 2013 at 03:20 PM 0
Share

So basically it's a big static variables container, it seems good. I'm gonna give it a test drive. But one thing I'd like to add first, is some counter-measures against the case where there is more than one instance of "__Blah" or "__AudioRelated". I like everything to be robust.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dynamic batching - different vertices 0 Answers

Calling a function from all instances of a script 1 Answer

SerializedObject and modification 1 Answer

Static target in the srcipt for my Prefab 2 Answers

How to design a static prefab class. 0 Answers


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