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 bennysc · Mar 17, 2013 at 01:54 AM · instantiateprefabgridhexagon

How can customize which prefabs are instantiated in a grid?

I'm trying to instantiate a prefab in a grid, and at certain coordinates in the grid I want to use a different prefab. For example I could generate the grid, but at 7,9 I could use the GameObject GrassHex instead of WaterHex.

The idea is that I can create a variable that randomly generates those coordinates, and then I can create a radius around that point. All objects in the grid within that radius will use a different prefab. This will create a "landmass" of GrassHex objects in the grid. Right now I have this script (this isn't the whole thing, just the instantiate part), and it generates the grid just fine, but it only uses one prefab.

WaterHex is the main hex prefab
gridHeightInHexes and gridWidthInHexes are public variables defined in the inspector (later on defined in game)

How can I define which points on the grid use a particular prefab?

  void createGrid()
         {
             GameObject hexGridGO = new GameObject("HexGrid");
     
             for (float y = 0; y < gridHeightInHexes; y++)
             {
     
                 for (float x = 0; x < gridWidthInHexes; x++)
                 {
                     GameObject hex = (GameObject)Instantiate(WaterHex);
                     //Current position in grid
                     Vector2 gridPos = new Vector2(x, y);
                     hex.transform.position = calcWorldCoord(gridPos);
                     hex.transform.parent = hexGridGO.transform;
                 }
             }
         }





Here is an example of what I get now, and here is a basic example of what I want to get.

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 Chronos-L · Mar 17, 2013 at 02:19 AM

I propose the following solution.

GetPrefab Function

 public static int WATER_HEX = 0, GRASS_HEX = 1 ..... METAL_HEX = 11;
 
 GameObject GetHex( int id ) {
    switch( id ) {
       case WATER_HEX:
          return waterHexPrefab;
       case ...
       ....
    }
 }

You can use this in 2 ways:

  • GameObject hex = Instantiate( GetHex(WATER_HEX) ) as GameObject;. Gives you controls on what you want to create.

  • GameObject hex = Instantiate( GetHex( Random.Range(0,12) ) ) as GameObject;. Randomize the hex.


I am using int here, so that the function can use Random. But I am quite sure that you can use Random with enum, but that needs a little bit more work.

Pros: Can do random generation and controlled generation

Cons: The switch in the function will become larger when you have more distinct hextiles.


To get a specific shapes, the only way I can think of is to use an array to store the formation. Something like this:

 int w = WATER_HEX , g = GRASS_HEX;
 int [][] formation = 
 {
  { w, w, w, w, w, w, w, w },
  { w, w, w, w, g, g, w, w },
  { w, w, w, g, g, g, w, w },
  { w, w, w, w, g, g, w, w },
  { w, w, w, w, w, w, w, w }
 };

You can then combine it with the above function:

 GameObject hex = Instantiate( GetHex( formation[x][y] ) ) as GameObject;
Comment
Add comment · 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

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

11 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

Related Questions

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

How to instantiate a prefab at mouse pos 1 Answer

Tiles Slow Down Game 0 Answers

How can I create a grid for a Leveleditor? 1 Answer

Instantiate prefab within parameters?(Javascript) 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