Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Best way to use prefabs to generate a tile in the world

Discussion in 'Scripting' started by Deleted User, Feb 4, 2015.

  1. Deleted User

    Deleted User

    Guest

    So I managed to create a data structure for a tile in a strategy game. It keeps track of properties like resources, terrain, and so on. It also holds onto some Vector2's for world coordinates. (The tiles are irregularly shaped voronoi polygons.)

    I did a really amateur job of actually turning that data into a game object in the world. The tile object has a method called "save to object" that creates a mesh in the world, using the Vector2's. I'm just using the script to crap out little gameobjects/meshes in the world. (Which is a pain, because those meshes don't contain the script component or any of the associated data.)

    I think I should be using prefabs? But I can't figure out the fundamental scripting.

    I have a "public class Tile { ... }" that stores the key info for each tile. It's a few integers, plus some intricate geometry performed by various methods.

    I also have a "public class TileMap { ... }" that generates the above tiles (giving them useful properties like resources, terrain, and so on).

    So in my larger TileMap script, there's a loop that generates one tile at a time. And there comes a moment where it's time to build a tile. (And I pass all the relevant integers / vector2's and so on.)

    Doesn't work. I clearly don't know what I'm doing, and I've read a dozen tutorials that haven't helped.
     
  2. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    Is your Tile class derived from MonoBehaviour?
     
  3. Deleted User

    Deleted User

    Guest

    I've tried both with and without and I feel like I'm beating my head against a wall.
     
  4. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    Do a Google of "new monobehaviour class" and take a look at a few of those topics. I think that's all it's going to take to get you on the right track.
     
  5. Deleted User

    Deleted User

    Guest

    Thanks for your help. I did some reading up on it, and now have some idea of when to use monobehaviors. I then sidestepped the issue completely by just generating empty GameObjects, and adding the script component to that. I genuinely appreciate the support.
     
  6. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    Good to hear you're on the right track! :)
     
  7. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    From the sounds of things, what you're facing is what most people face - getting to grips with the component/behaviour based way in which Unity works. It's a little odd at first if you're coming from prior programming experiences.