Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MountDoomTeam · Sep 22, 2012 at 06:18 PM · prefabloadresourcescreate

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Sep 22, 2012 at 07:28 PM 0
Share

Just to make things clear: is this really an Editor script? Only Editor scripts can modify prefabs

avatar image MountDoomTeam · Sep 22, 2012 at 07:39 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prefab item placment with code? 1 Answer

Resources.loadall not loading prefabs at runtime. 1 Answer

Altering prefab at runtime. Works in editor, not in build. 2 Answers

Have some strange problem with Resources.Load 1 Answer

Can a prefab be written in code? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges