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 boddole · May 04, 2014 at 12:56 AM · c#editorprogrammingserialization

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.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
 
         }
 
 
     }
Comment
Add comment · Show 2 · Share
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 boddole · May 04, 2014 at 02:55 AM 0
Share

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"

avatar image RyanPaterson · May 04, 2014 at 12:55 PM 0
Share

I edited my first post. It's at the bottom. Hope it helps

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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