You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
2 Hrs
11 Min
21 Seg

Sistema de coordenadas para instanciar las piezas

10/34
Resources

How to fill our parts grid in Unity?

Welcome to this exciting lesson on how to manage your parts grid in Unity. Throughout this process, we will delve into two fundamental aspects: the coordinate system and the pieces that will be positioned on it using tiles. Let's dive into the technical details that facilitate this process.

How to create the coordinate system?

The first crucial step is the establishment of our coordinate system. This system will be the dawn of our grid in Unity, where each space is clearly identified. Initially, we will need a new script:

  1. Script Creation:

    • Access your scripts folder in Unity.
    • Right click, select create new C Sharp script and name it Tile.
    • Open your script in Visual Studio to make the necessary modifications.
  2. Script Properties: Within the Tile class, define the following properties:

    public int X;public int Y;public void Void;
    • These properties will represent the X coordinate, the Y coordinate and a reference to the grid.
  3. Setup function:

    • Omit the Start and Update functions.
    • Implement a setup function that receives these properties as parameters.
    public void Setup(int x, int y, void myVoid) { X = x; Y = y; Void = myVoid;}
    • This function will assign the relevant values to each tile in the grid.

How to assign the component to the prefabs?

Once you have created the Tile script, it is time to integrate it with your prefabs in Unity. This will allow each space in your grid to be represented properly.

  1. Prefab configuration:

    • Go to the prefabs folder.
    • Select the empty prefab and add the Tile component to it.
    • Remember that these values will be dynamically assigned later in the grid script, not directly in the prefab.
  2. Edit the Grid Script:

    • Locate the Void script and edit the SetupVoid method, which is where the grid spaces are created.
    • Use GetComponent<Tile>() to access the Tile component of each instantiated object.
    • Be sure to handle possible errors in accessing the component using the null conditional operator ?....
  3. Coordinate Assignment:

    • Call the setup function of the Tile component.
    tileComponent?.Setup(x, y, this);
    • Here, x and y are the coordinate positions, and this is used to reference the current grid.

What results do we expect?

Go back to Unity and test your setup. When you run your project:

  • Notice that the grid is still displayed as expected.
  • If you select any element in the grid, you now see that they have the Tile component assigned with the correct coordinates.
  • The lower left space will be the coordinate 0, 0, ascending progressively.

Each of these objects now not only represents graphically a space in the grid, but also has a meaning within Unity and the coordinate system. This system prepares us to integrate, in a future class, the pieces that will occupy these spaces, ensuring that each one aligns correctly with its position on the grid.

Keep exploring and believe in your abilities; you are closer to having a fully functional game. Come on, you can do it!

Contributions 0

Questions 1

Sort by:

Want to see more contributions, questions and answers from the community?