I am having issues with selecting units in a turn based strategy game I am building. Below is the script in question. My issues are that for some reason selectedUnit and selectedContainer are not being set when the raycast is being shot out.
void Update () {
if (Input.GetMouseButtonUp (0)) {
Debug.Log ("click");
if (Physics.Raycast (detectUnit, out hitUnit, Mathf.Infinity, 8)) {
//if no unit is selected
if (isSelect == false) {
//set the selectedUnit gameobject equal to the object hit by the raycast
selectedUnit = hitUnit.transform.gameObject;
//set selected container equal to the container script on that object
selectedContainer = selectedUnit.GetComponent<container>();
//if it is player 1's turn
if (currentPlayer == 0) {
//if unit is under player 1 control set selected flag in this script and unit's container script to true
if (selectedContainer.player == 0) {
selectedContainer.isSelected = true;
isSelect = true;
}
//if unit is not under player 1 control, do nothing
if (selectedContainer.player == 1) {
selectedContainer.isSelected = false;
isSelect = false;
}
}
//if it is player 2's turn
if (currentPlayer == 1) {
//if unit is under player 2 control set selected flag in this script and unit's container script to true
if (selectedContainer.player == 1) {
selectedContainer.isSelected = true;
isSelect = true;
}
//if unit is not under player 2 control, do nothing
if (selectedContainer.player == 0) {
}
}
}
//if a unit is selected
if (isSelect == true) {
//if it is player 1's turn
if (currentPlayer == 0) {
//if unit is under player 1 control, deselect old unit and select new unit
if (selectedContainer.player == 0) {
previousSelected = selectedContainer;
selectedUnit = hitUnit.transform.gameObject;
selectedContainer = selectedUnit.GetComponent<container>();
selectedContainer.isSelected = true;
previousSelected.isSelected = false;
}
//if unit is not under player 1 control, do nothing
if (selectedContainer.player == 1) {
}
}
//if it is player 2's turn
if (currentPlayer == 1) {
//if unit is under player 2 control, deselect old unit and select new unit
if (selectedContainer.player == 1) {
previousSelected = selectedContainer;
selectedUnit = hitUnit.transform.gameObject;
selectedContainer = selectedUnit.GetComponent<container>();
selectedContainer.isSelected = true;
previousSelected.isSelected = false;
}
//if unit is not under player 1 control, do nothing
if (selectedContainer.player == 0) {
}
}
}
}
}
}
}