- Home /
Linking prefabs to classes that are not attatched to gameobjects
I'm learning to use classes using java. Im trying to create a system to manage npc spawning etc. Basically im creating a class and instantiating it in my main game script which is attached to a gameobject.
i want to assign a bunch of prefabs to various transform variables in my class. My class is not attached to an object but the interface allows you to hook up prefabs to the variables in the class itself.
Now i have this code in the class.
class MyClass { public var thisPrefab : Transform;
function MyClass()
{
}
function getPrefab()
{
return thisPrefab;
}
}
now in my main game script i and trying to basicaly do this
var npc = new MyClass();
function foo()
{
enemyPrefab = npc.getPrefab;
Instantiate(enemyPrefab, enemySpawn.transform.position, enemySpawn.transform.rotation);
}
Im getting the error "the prefab you want to instantiate is null.
I know your supposed to connect the prefab to the script once its attached to a gameobject but with a class i dont see how to do this. Not sure where to go from here. Is there a way to dynamically choose prefabs without manually linking them in the editor?
Thank you. Jesse
http://www.ericgiguere.com/articles/javascript-is-not-java.html You don't need to use position : Vector3, rotation : Quaternion. Just use the one-parameter version of Instantiate once you get it working.
Answer by Alec-Slayden · Feb 28, 2011 at 11:35 PM
if you create a folder called "Resources" in your assets, you can use Resources.Load