c# - seting universal spawn prefab -



c# - seting universal spawn prefab -

we've been banging our heads one, in theory, having public static object hold prefab spawned should have solved this, script gets static variable when static keyword not there, still objects individual variables, while when add together static keyword, script's variable disappears. doing wrong? exact definition:

public static object prefab;

i still new unity, offer personal research / findings. hate explicitly have object public properties exposed can "stick" object prefab in scene. if trying build scheme / game components dynamically, knowing / when add together discretion. liked approach of having static available can go @ time needed "thing".

i found using "resources" allow load prefab via code without need of having scene object drag controls to, such master list.

in default "assets" folder, create folder "resources" (not sure if case-sensitive). anyhow, set other stuff in respective folders under that... materials, prefabs, sprites, textures, whatever... now, in "prefab" folder, have prefab "hero", "laser", "shield", whatever.

now, how these pieces static class. create special method phone call load them via resources class of unity, then, create function homecoming clone of item in question. way, leave original alone, , have own work in game needed.

something like...

using system; using system.collections.generic; using system.linq; using system.text; using unityengine; using system.collections; namespace assets.scripts { public static class mygameobjmanager { private static bool alreadyloaded; public static gameobject myhero { get; private set; } public static gameobject myweapon {get; private set;} public static void loadobjects() { if (alreadyloaded) return; // load resource relative "resources" path... myhero = (gameobject)resources.load("prefab/hero"); // other appears load @ specific path allowing go root level assets myweapon = (gameobject)resources.loadassetatpath("assets/resources/prefab/weapon", typeof(gameobject)); alreadyloaded = true; } } public class gamemgr2 : monobehaviour { public static gameobject gethero() { mygameobjmanager.loadobjects(); homecoming (gameobject)instantiate(mygameobjmanager.myhero, new vector3(0, 0, 0), new quaternion()); } public static gameobject getweapon() { mygameobjmanager.loadobjects(); homecoming (gameobject)instantiate(mygameobjmanager.myweapon, new vector3(0, 0, 0), new quaternion()); } } }

so, static class can not derive monobehavior allows "instantiate" phone call create clone of object , homecoming it. so, created secondary non-static class derived monobehavior , set static methods phone call static game object manager prefabs need.

again, still learning, hope helps design considerations... calling hero or weapon, parameterize passing in vector3 coordinate, or quaternion value, can run it.

static vs not

per code , making static means single instance no matter how many of class set anywhere within system. if have 5 prefabs on screen , seek set each 1 different "thing", overwrite whatever 1 assigned last. having property "public" allow have 5 prefabs on screen , drag/drop different "things" on each respectively , each retain own "thing" instance.

sorry can't advise more without knowing trying implement.

non-static should help within editor drag/drop. however, static

c# unity3d static-members

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -