Home > Mobile >  How to fix DisplayInventory.cs(17,54): error CS0246: The type or namespace name 'GridLayoutGrou
How to fix DisplayInventory.cs(17,54): error CS0246: The type or namespace name 'GridLayoutGrou

Time:02-01

I am trying to create inventory in unity3D, and I present a variable named obj and I try to get his parent component (Grid Layout Group) , But I am getting this error: Assets-Scriptable Objects\Inventory\DisplayInventory.cs(17,54): error CS0246: The type or namespace name 'GridLayoutGroup' could not be found (are you missing a using directive or an assembly reference?) My Code :

public GameObject inventoryPrefab;
private void Start()
    {
        for (int i = 0; i < inventory.Container.Items.Length; i  )
        {
            var obj = Instantiate(inventoryPrefab, Vector3.zero, Quaternion.identity, transform);
            var objParent = obj.GetComponentInParent<GridLayoutGroup>();
            objParent.cellSize = new Vector2(45, 45);
            objParent.spacing = new Vector2(6, 6);
            itemsDisplayed.Add(obj, inventory.Container.Items[i]);
        }
    }

CodePudding user response:

How to solve this

You have missing a reference to the namespace which contains the class GridLayoutGroup. If you hover your mouse over the red line under GridLayoutGroup (provided you use visual studio as your IDE) then a light bulb should appear. Hover your mouse over the light bulb, you will have an option like "Using UnityEngine.UI" click on it and your problem will be solved.

You can do this manually as well, just go to the start of your file, make an empty line and type: Using UnityEngine.UI

Why is this happening?

The compiler divides classes into logical units called namespaces. This helps keeping your project organized, making it easier to code. If you want to acces a namespace from outside, you have to either specify the namespace from which you want your class (UnityEngine.UI.GridLayoutGroup) or add the using statement to the file, so whenever you type a class name, the compiler will look for that class in all the included namespaces as well.

  •  Tags:  
  • Related