• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Help with Missing Monobehaviours and Asset Serialization? 0 Answers

How do I create variables through the editor to use at runtime? 0 Answers

CustomPreview example? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges