Hex grid cube coordinates

Hi, any tips for assigning each node a cube coordinate system ie 3 axes QRS. I’m struggling to get the formula for the S coordinate correct. For reference I am following a tutorial on Hexagonal Grids cube coord system.

void GenerateGridCells() {
var nodes = AstarPath.active.data.gridGraph.nodes ;
foreach ( GridNodeBase node in nodes ) {
var nodePosition = ( Vector3 ) node.position ;
var gridCell = Instantiate( _gridCell , nodePosition , Quaternion.identity ) ;
gridCell.transform.parent = transform ;

				var xCoord = node.XCoordinateInGrid ;	
				var zCoord = node.ZCoordinateInGrid ;
				
				var q = xCoord - ( zCoord + ( zCoord % 2 ) ) / 2 ;
				var r = zCoord ;
				var s = -q - r ;

				gridCell.name = $"GridCell: {q} , {r}, {s}" ;
			}
		}

This code returns correct Q and R but S is not correct (with the goal that all coordinates add up to equal zero). The reason is to allow for grabbing certain tiles e.g. a ring using established formulas with the cube coord system.

Well, if you say that q + r + s = 0 and you get correct q and r. Then s must be correct as well because there’s only one possible value of s that makes them add up to zero.