- Home /
Instantiating Prefabs through Editor Script
Hello everyone, I've been looking around for info on editor scripts / menu items, but I just have not come across the information I need to get this done.
What I'm trying to do: I would like to be able to feed a editor script prefabs and transform locations (the transforms already exist in the game scene as empty game objects), and then have the script position the elements of the prefabs based on the transform data (and of course save the data).
From what I've read, the prefabs can be loaded and I'm fine with adjusting the prefabs according to the transform data once they are created. But as far as feeding the transform locations into an array, I have no solution for that, and I'm still confused on exactly what I would need to make serializeable so that everything isn't lost when the game is played.
If anyone would point me in the right direction, I would appreciate it.
Answer by RyanPaterson · May 04, 2014 at 01:18 AM
You want to load prefabs to transform locations?
try something like this: (note i havent tested this)
public Transform[] transformArray;
That creates an array that can hold a collection of the type - transform. Then in the inspector, place your transforms into it. 'Lists' can also be used, you'll find them both in the docs.
Then when you load a prefab ( feed it some element of the array e.g. Instantiate is what you use to load a prefab from script, if you use resources.load to find an object via script, you need to have your prefabs in a folder titled 'Resources' so your project folder would look like this:
ProjectFolder > Assets > Resources. The for loop just iterates through the length of the array. (This may not be the desired functionality, but you should be able to get it from this
for(int i = 0; i < transformArray.length; i++){
GameObject object = Instantiate(Resources.Load("object"), transformArray[i], Quaternion.identity) as GameObject;
}
Hope that helps, if not let me know.
EDIT:
From your comment, I thought i'd post it here so we can get this solved asap. From experience only in arrays (which is limited) - You will lose the inspector functionality that you would have as public.
Solution: Try creating a public array, and then a second static array, and having the second = the first.
E.g.
public Transform[] firstArray;
public static Transform[] secondArray;
void Start(){
secondArray = firstArray;
for (int i = 0; i < secondArray.Length; i++) {
GameObject newObject = Instantiate(Resources.Load("Cube"), secondArray[i].position, Quaternion.identity) as GameObject;
}
}
I've tried more/less exactly what you suggested earlier, the problem is that when I try to use the array inside the static method I get reference errors (CS0120) "an object reference is required for the non-static field, method, or property"
I edited my first post. It's at the bottom. Hope it helps
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Big Problem Serializing 0 Answers
Serialize Texture3D 1 Answer
Trouble setting the object reference in a property drawer 0 Answers
How can an editor script know when another script was removed from the project? 1 Answer