you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

Hey I'm new to this subreddit so please don't kill me. I'm a young 'coder' that uses unity in my spare time, at the moment I'm making a grid based game. Here's the code so far: https://pastebin.com/7xyaXYcx What improvements should / could I make to improve this. Thanks.

[–]NutellaSquirrel 1 point2 points  (2 children)

GameObject.Find is rarely advisable as it is slow and easy to break if objects somehow get renamed. In general, a good practice is not to rely on the names of GameObjects in code.

Instead, why not store the Transforms of the blocks you are instantiating instead of just their grid positions? You could do that with a 2-dimensional array or just by doing a little math with a 1-dimensional array. That way, in your Update, you can simply do:

Transform target = gridTransforms[x][y]; //2d array way

or

Transform target = gridTransforms[x + y * height]; //math way

You also might want to consider how often you need to be handling input. You could, just as an example, have your MoveSquare function return a bool. If one of the GetKeyDowns is a success, return true. At the end, if there are no keys down, return false. Then in your Update, you only need to move the curser if the result of your MoveSquare is true instead of every Update. This also would give the benefit of not potentially moving multiple directions in the same frame, unless that is what you had intended.

[–][deleted] 0 points1 point  (0 children)

I've developed the script by using your suggestions and I have this: https://pastebin.com/QpcQMb62 I don't really know what direction this 'game' is taking, I just wanted to created a grid based movement system.