Home > Back-end >  How do I convert between enums for passing between different functions
How do I convert between enums for passing between different functions

Time:11-19

I have enums set up in a script called Grid, as follows:

public enum CellType
{
    Empty,
    Road,
    Structure,
    SpecialStructure,
    None
}

Then I have a script called placement manager which adds data relevant to that enum like this:

internal void PlaceObjectOnTheMap(Vector3Int position, GameObject structurePrefab, CellType type, int width = 1, int height = 1)
    {
        StructureModel structure = CreateANewStructureModel(position, structurePrefab, type);

    var structureNeedingRoad = structure.GetComponent<INeedingRoad>();
    if (structureNeedingRoad != null)
    {
        structureNeedingRoad.RoadPosition = GetNearestRoad(position, width, height).Value;
        Debug.Log("My nearest road position is: "   structureNeedingRoad.RoadPosition);
    }

    for (int x = 0; x < width; x  )
    {
        for (int z = 0; z < height; z  )
        {
            var newPosition = position   new Vector3Int(x, 0, z);
            placementGrid[newPosition.x, newPosition.z] = type;
            structureDictionary.Add(newPosition, structure);
            DestroyNatureAt(newPosition);
        }
    }

}

finally, I have another script called Grid Helper which is supposed to call placement manager and add itself to the grid which I've set like this:

public enum CellType
    {
        Empty,
        Road,
        Structure,
        SpecialStructure,
        None
    }
    [SerializeField]
    private CellType structureType = CellType.Empty;

public PlacementManager placementManager;
// Start is called before the first frame update
void Start()
{
    placementManager.PlaceObjectOnTheMap(new Vector3Int(Mathf.FloorToInt(transform.position.x),
        Mathf.FloorToInt(transform.position.y),
        Mathf.FloorToInt(transform.position.z)), this.gameObject, structureType);
}

but I somehow keep getting told Argument3 cannot convert from GridHelper.CellType to CellType.

What am I doing wrong?

CodePudding user response:

Remove your enum in the GridHelper or in the Grid class and use GridHelper.CellType or Grid.CellType in the other class depending on what you removed.

  • Related