- Home /
Creating prefab by script and using it crashes unity
answer: you can make a script that changes a prefab in a normal scripts and it will kindof work a tiny bit but it's for an Editor Script.
its a script to stack 4 cubes, create an empty prefab and put the 4 cubes in it, then it instantiates the prefab with 4 cubes around in space. it's all fine until i try to instantiate the prefab i just created(at the end), it works and then it goes into a memory loss/crash. Please add a cube prefab to the script and an Assets/Resources folder and it should run.
@script RequireComponent(MeshFilter)
var rows = 4;
var blocks : Transform;//drag a cube to the editor
private var rot : Quaternion;
var sp : GameObject;
function Start ()
{
var big : GameObject = gameObject;
big.transform.position = Vector3(0,0,0);
for (var q = 0; q < rows; q++) //make a stack of cubes
{
var displace = Vector3(0,q*1.1,0);
rot.eulerAngles = Vector3(0,0,0);
var ino = Instantiate(blocks, displace, rot);
ino.transform.localScale = Vector3(1,1,1);
ino.transform.parent = big.transform;
}
//put stack of cubes into a new prefab in /Resources
var prefab : Object = EditorUtility.CreateEmptyPrefab("Assets/Resources/pack1.prefab");
EditorUtility.ReplacePrefab(big, prefab, ReplacePrefabOptions.ConnectToPrefab);
var go : GameObject = (Resources.Load("pack1"));//Load the prefab
// for (var r = 0; r < rows; r++)
// {
var displace2 = Vector3(1.1,0,0);
var ino2 = Instantiate(go, displace2, rot);//make a second stack next to the first
ino2.transform.localScale = Vector3(1,1,1);
//}
}
I'm making some procedural geometry experiments where i copy bunches of blocks into different symmetries. So i am trying to use the Prefab as a kind of local memory so i can copy and paste arrangements of meshes. It's an attempt to make a modular code.
Just to make things clear: is this really an Editor script? Only Editor scripts can modify prefabs
thanks you're totally accurate there. and if i place it in editor folder i cant attach it to an object.
do you mean i have to make a GUI extension for it or i just have to place it in Editor folder? The above script does actually create a prefab in resurces, and puts the cubes in it, and it does also load it back onto the game, but then it loads 20x the instantiation line even though it's only written once, and it freezes. i try to figure out editor scripts. thanks.