foxjasond
foxjasond

Reputation: 63

Getting height/width from a unity gameobject

I'm working on a version of Conway's Game of Life in Unity, and here is my setup for making the grid:

I've created a prefab of an individual cell responding to mouse click that will be the basis for creating the cell grid. I have a Empty GameObject to act as the controller to create the grid. I'm putting it in the code for the controller like so, pointing my prefab to the field:

[SerializeField]
private GameObject Cell;

private Camera _camera;

My idea was to get the dimensions of the Cell and instantiate it into a grid, with _camera pointing to the Main camera to get boundaries. However, I'm not sure how to get the height/width from GameObject. What's the best way to find this out?

Upvotes: 6

Views: 33821

Answers (1)

Dommar92
Dommar92

Reputation: 325

I don't know if you found the answer, but the most common way is using Collider (if you have one, but mouse click needs it) or Renderer (if you have a mesh) by using:

GetComponent<Collider>().bounds.size
GetComponent<Renderer>().bounds.size

Game of life is very nice, I've written my paper for bachelor seminar about it. Have fun!

Upvotes: 9

Related Questions