- Home /
How can I instantiate a prefab from a static function?
Hi everyone, I have a script which 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;
}
}
The prefab as to be static as well in order for you to use it in your static function.
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 Game$$anonymous$$anager
, or Atlas$$anonymous$$anager
, etc. Then at least use a Singleton.
You mean instantiate a prefab, and have a variable reference it? How about doing it in Awake
?
"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.
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.
As to how you can easily start your game from that special "tiny scene", this question has a few ideas.
just by reading the comments in the answer lol makes me feel that the whole world is safe XD - $$anonymous$$an @Fattie I wish you'd be a president or something, that way we would have less evil, and live $$anonymous$$ISSly ever after :D
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 :/
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.
Your answer
Follow this Question
Related Questions
Coroutine cannot be automatically started from a static function 1 Answer
How to use a float value from coroutine 1 and use in coroutine 2? 1 Answer
Object reference to set to instance on an instantiated object 1 Answer
Static and Coroutines help 2 Answers
Call methods on start of Application 2 Answers