void Update() { Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Input.GetMouseButtonDown(1)) { if (Physics.Raycast(mouseRay, out hit, 1000f, layerMask, QueryTriggerInteraction.Ignore)) { if (p != null && l != null) { Reset(); } p = hit.transform.GetComponent <Placeable>(); l = p.GetComponentInChildren <Light>(true); l.enabled = true; } } if (p != null && !p.isPlaced) { if (gameManager.spearSelected) { curTower = towerPrefabs[0]; towerHeight = 1.03f; } if (gameManager.archerSelected) { curTower = towerPrefabs[1]; towerHeight = 0.65f; } if (gameManager.cannonSelected) { curTower = towerPrefabs[2]; towerHeight = 1.13f; } if (gameManager.spearSelected || gameManager.archerSelected || gameManager.cannonSelected) { Debug.Log("Spawning a " + curTower.name); towerPos = new Vector3(p.transform.position.x, towerHeight, p.transform.position.z); GameObject clone = Instantiate(curTower, towerPos, Quaternion.identity, GameObject.Find(curTower.name + "s").transform); clone.GetComponent <Tower>().enabled = true; p.isPlaced = true; // clone.name = curTower.name; l.enabled = false; gameManager.spearSelected = false; gameManager.archerSelected = false; gameManager.cannonSelected = false; ClearCache(); } } }
void ClearCache() { p = null; l = null; curTower = null; }