• 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 /
This question was closed Jul 23, 2015 at 08:15 PM by Noxury for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Noxury · Jul 23, 2015 at 08:06 PM · editorinstantiateprefabgridlevel

How can I create a grid for a Leveleditor?

Hello,

I want to make a grid, but the instantiated grid-prefabs are not aligning right next to each other, they are overlapping:

Gridfail

 for(var x : int = 0; x < sizeX; x++){
     for(var y : int = 0; y < sizeY; y++){
         gridClone = Instantiate(grid, Vector3.zero, Quaternion.identity);
         gridClone.transform.position = Vector3(x,0,y);
     }
 }


gridfail.png (22.6 kB)
Comment
Add comment · Show 1
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 Cherno · Jul 23, 2015 at 08:15 PM 0
Share

Looks like you need to take into account the size of the prefab. Try multiplying the x and y values of the position by about 16.

 gridClone.transform.position = Vector3(x * 16,0,y * 16);

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Noxury · Jul 23, 2015 at 08:14 PM

Nevermind, I have found a solution:

 for(var x : int = 0; x < sizeX; x++){
     for(var y : int = 0; y < sizeY; y++){
         gridClone = Instantiate(grid, Vector3(x*24,0,y*24), Quaternion.identity);
     }
 }

}

Note that 24 is the width/height of the grid prefab.

Comment
Add comment · Show 1 · 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 Jessespike · Jul 23, 2015 at 08:17 PM 0
Share

Need to provide how much space is in between the instantiated objects. Can make public properties and adjust as needed:

 public float spacingX = 10f;
 public float spacingY = 10f;

then in the for-loop, multiple the position with the spacings:

 gridClone.transform.position = Vector3(x * spacingX, 0f, y * spacingY);


Note that the variable "y" is actually using the Z position. I named the property |spacingY| to keep consistent with the existing code. It should be "z".

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

22 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

Related Questions

Add prefabs to hierarchy without game running? 1 Answer

Trying to create grid, "Use of unassigned local variable" error for prefab of tile although it is assigned? 0 Answers

Editor Script: Linking GameObjects to public script variables resets when playing. 1 Answer

Why my prefab is destroyed after reloading level? 2 Answers

Instantiate prefab within parameters?(Javascript) 1 Answer

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