Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ltrout1999 · Mar 24, 2016 at 10:58 PM · gameobjectprefab

Changing multiple identical gameobjects into one prefab?

I'm creating a game based on the Minecraft game 'Spleef'. I am doing a prototype currently, but I placed a plane, put a cube on it and copied and pasted that cube to fill up the plane. I made this a larger plane so there are 400 cubes currently on it. I renamed all of the cubes to 'Cube' and drug one into a Prefab folder to create a prefab out of it. I need to use a prefab in my script, and I don't want to have to go back and manually change each cube to the prefab version. Is there anyway to change all of the gameobjects named Cube to the prefab? Or how could I do this easier than placing and moving each cube one by one again?

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
Best Answer

Answer by dhore · Mar 25, 2016 at 01:32 AM

Hey @ltrout1999 :) good to see you again

I've just spent about an hour playing around with a script for you, so here it is:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 public class PlaceCubes : MonoBehaviour {
 
     [MenuItem("MyMenu/PlaceCubes ")]
     static void Place()
     {
         // change these to work for you:
         string prefabName = "Cube"; // case sensitive
         Vector3 gridSize = new Vector3(10, 1, 5); // ie. (10, 1, 5) will generate 10 cubes in the x direction, 1 in the y, and 5 in the z
         Vector3 offset = new Vector3(1, 1, 1); // size of the cubes - default is (1, 1, 1)
         string newCubeName = ""; // leave blank for default name eg. "Cube(Clone)"
 
 
         GameObject cube = Resources.Load(prefabName, typeof(GameObject)) as GameObject;
 
 
         for (int x = 0; x < gridSize.x; ++x)
         {
             for (int y = 0; y < gridSize.y; ++y)
             {
                 for (int z = 0; z < gridSize.z; ++z)
                 {
                     Vector3 pos = new Vector3(x * offset.x, y * offset.y, z * offset.z);
                     GameObject go = Instantiate(cube, pos, cube.transform.rotation) as GameObject;
 
                     if (newCubeName != "")
                         go.name = newCubeName;
                 }
             }
         }
     }
 }

So what this'll do is it'll provide a Menu at the top (where File, Edit, etc are) called "MyMenu" with a Menu Item called "PlaceCubes", which when you click it will place cubes in the scene. (Note: the script does not need to be attached to any gameobjects to work).

There are some 'settings' at the top of the function which you'll need to manually change in the script to work to your liking - they are:

  • "prefabName" : the name of the prefab object. Note: the prefab must be in a folder called "Resources". If the object is then in a subfolder of Resources then this name should be the path to the object (using forward slashes). eg. if your prefab is at Assets/Resources/Prefabs/Cube then this name should be set to "Prefabs/Cube"

  • "gridSize" : the amount of cubes you want to generate in each direction.

  • "offset" : the size of the cube - used to space them apart (which can literally be used for spacing if you want).

  • "newCubeName" : a name for your new cubes.

I've tested this and it works fine on my end. But let me know if you have any issues :)

Enjoy, dhore.

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 ltrout1999 · Mar 25, 2016 at 08:29 PM 0
Share

Seems to work fairly well! Thank you!

Question, would there be anyway I could make it to where ins$$anonymous$$d of inputting the size in the code, to click on the button and it ask for a size there? That way I could use one code to quickly create a certain amount of blocks w/out having to change the code each time.

avatar image dhore ltrout1999 · Mar 28, 2016 at 05:32 AM 0
Share

Not a problem :)

There probably is a way to do that, but I didn't have time to go looking when I wrote it as I was about to head out for the weekend. I also kind of wanted to add that feature too, but yeah.. no time which sucked...

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

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

Related Questions

Place prefab with mouse on to a grid based surface with a correct orientation. c# 0 Answers

List of unused game objects and prefabs 1 Answer

Prefab destroyed upon instantiation 0 Answers

How to recover missing prefab?,How to recover missing prefabs? 0 Answers

All instantiated objects having same location, 0 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