- Home /
GetComponent not working on GridLayoutGroup
I have this little script that is supposed to take a UIObjects RectTransform height (works) and then divide it by 5 and make it the cell size for the GridLayoutGroup (not working).
This is my code:
public GameObject gridLayout;
private float newCellSize;
void Update () {
newCellSize = gridLayout.GetComponent<RectTransform>().rect.height/5;
gridLayout.GetComponent<GridLayoutGroup>().cellSize = new Vector2(newCellSize, newCellSize);
}
For some reason my script isn't recognizing the GridLayoutGroup component (before I even run my script). I have linked the gameObject in the inspector window. Thanks in advance!
Answer by Bunny83 · Aug 19, 2015 at 11:16 AM
The class GridLayoutGroup is defined in the UI namespace. To access it you have to either:
Add a
using UnityEngine.UI;
at the top of your scriptdirectly specify the full qualified class name
GetComponent<UnityEngine.UI.GridLayoutGroup>()
If you use multiple things from the UI namespace, the first solution is the usual one.
Thanks for the answer but I already found it myself before seeing yours. I'll make your answer the right one since it is the most detailed.
Answer by Florensie · Aug 19, 2015 at 11:14 AM
Sorry, seems like I had to inherit from UnityEngine.UI by adding using UnityEngine.UI;
Your answer
Follow this Question
Related Questions
How can I create a grid for a Leveleditor? 1 Answer
Beginner Help: hex grid set up for battlefield 0 Answers
3D level generator on a grid C# 1 Answer
How To Sync Animation To Music or Bpm ? 2 Answers
Grid Letter Box is not expanding, 0 Answers