#I need help for "for" script

1 messages · Page 1 of 1 (latest)

burnt vortex
#

so i wrote this code to make 25 square with distance but when i start the game list says there is 25 squares but in screen there is only 21 square that instantiated (btw when i try to increase the "if(rightNumber)" part square number is increasing)

public class GameManager : MonoBehaviour
{
public List<int> Tile = new List<int>();
public List<int> MineTiles = new List<int>();
public GameObject InstantiatePosition;
public GameObject TileGameObject;
public GameObject Canvas;
private int TileNumber = 25;
private int MineNumber = 3;
public int rightNumber = 0;

public void Start()
{
    for (int tileNumber1 = 0; tileNumber1 < TileNumber; tileNumber1++)
    {
        if (rightNumber < 5)
        {
            GameObject instantiatingTile = Instantiate(TileGameObject, InstantiatePosition.transform);
            instantiatingTile.transform.parent = Canvas.transform;
            InstantiatePosition.transform.position += new Vector3(100, 0);
            rightNumber++;
        }
        else if (rightNumber >= 5)
        {
            rightNumber = 0;
            InstantiatePosition.transform.position = new Vector3(InstantiatePosition.transform.position.x - 500,
                InstantiatePosition.transform.position.y - 100);
        }
        Tile.Add(tileNumber1);
    }
}
sand oak
#

why do you do this weird rightNumber check

burnt vortex
#

to make this square 5x5

#

5 to right 5 to under

sand oak
#

that's horrible

burnt vortex
#

i am new at programming

sand oak
#

just use divide/remainder

burnt vortex
#

okay thanks i will look at it

sand oak
#

for a given index i and width w
x = i % w
y = i / w

burnt vortex
#

can you make a example with my script

sand oak
#
const int gridWidth = 5;
const int tileSize = 100;

int squareSize = gridWidth * gridWidth;
Vector3 startPosition = /* fetch the initial position however you need to */;

for (var index = 0; index < squareSize; index++)
{
    int x = index % gridWidth;
    int y = index / gridWidth;

    var spawnPosition = new Vector3(x * tileSize, y * tileSize);
    GameObject tile = Instantiate(prefab, startPosition + spawnPosition, parent);
}
#

(you can specify the parent in the Instantiate call, you don't need to set .parent afterwards)

sand oak
#

oh you probably need to pass rotation too

#

you can just do Quaternion.identity ig

burnt vortex
#

thank you so much it worked

sand oak
#

you're welcome

#

Do you understand it

#

Because I am happy to clarify if you don't understand some of it (you said you were new)

#

I assume you do, because my code was example code and couldn't have worked if you just copy/pasted it blindly

burnt vortex
#

i barely understand it

#

i copy pasted it but i change somethings in the code to make it work

blissful granite
sand oak
#

because the type is evident

blissful granite
#

Like is there any benefits to var, except like laziness

sand oak
#

Guidelines state to use var when type is evident

#

which in this case it is, because it's a Vector3 constructor invocation

blissful granite
sand oak
#

And the type is evident when either
a) you're invoking a constructor
or b) assigning a literal. (for example var x = 32; is obviously int, because 32 is an int literal and there's nothing else x could be)

blissful granite
sand oak
#

there's zero performance difference, it compiles the same. var is just to make your code cleaner

#

Especially when you deal with things like nested generics

Dictionary<int, List<string>> dictionary = new Dictionary<int, List<string>>();

^ that's just horrendous. var means you don't have to repeat that nonsense

var dictionary = new Dictionary<int, List<string>>();
burnt vortex
#

is this chat stays forever because if i forget it i need to look this conversation

sand oak
#

The post will always be here as long as you or a staff member doesn't delete it. And staff have no reason to reason to delete it, so... as long as you keep it around it'll stay around

#

But it will be archived after some time of inactivity. So save a link if you need to

blissful granite
sand oak
burnt vortex
#

nah i am understanding it

sand oak
#

cool

burnt vortex
#

thank you so much for everything

#

have a nice day

sand oak
#

yw

#

you too

burnt vortex
#

i have another question can you help me

sand oak
#

Create a new post for it, if it's unrelated to this one

burnt vortex
#

its kinda related

sand oak
#

hm

#

what is it

burnt vortex
#

so we instantiated them eith listing and i use prefab for button so how do i understand which one am i clicking on list

sand oak
#

You could set their name when you instantiate them

#

using the index, I suppose

burnt vortex
#

how do i get their name by clicking

sand oak
#

It'll highlight in the Hierarchy

burnt vortex
#

no i mean how do i get their name in script by clicking

sand oak
#

oh

burnt vortex
#

i create onclick using gamemanager and i want to get their name in gamemanager script when i click the button

sand oak
#
  • detect when the player clicks the object (a few ways to do this. OnMouseDown if the object has a collider. you could also use raycasts)
  • get the object's name (GameObject has a name property, you can just read that)
  • do what you plan to do with the name
burnt vortex
#

they dont have colliders they are button i want to do it with onclick

sand oak
#

🤔

#

are you using UI elements to make your game?

burnt vortex
#

am i asking too much

#

actually its basicly not a game

#

minigame

#

like minesweeper

sand oak
#

still, you shouldn't use UI for actual game elements

#

I should have suspected as much when I saw you were setting the parent to Canvas.transform

#

That was immediately sus

#

Unity has 2D support, it has sprites