private void Start() { price = item.GetStats().price; text = Instantiate(text, transform); UnitStats s = item.GetStats(); text.text = item.unit.name + "\nCost: " + s.price.ToString(); }
bool PlaceUnit() { UnitStats stats = heldUnit.GetStats(); // checks the place under your cursor and returns a RaycastHit if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, 25f)) { // checks if the collider that got hit is suitable for the unit if (hit.collider.CompareTag(stats.terrainTag)) { // collects all the colliders within a sphere Collider[] Obstacles = Physics.OverlapSphere(hit.point, stats.size); foreach (Collider col in Obstacles) { // checks all the gathered colliders and cancels function if the unit can't fit if (!col.CompareTag(stats.terrainTag)) { if (!col.CompareTag("Ground")) { return(false); } } } // after passing all checks for a suitable spawn, spawn a copy of the prefab Instantiate(heldUnit.unit, hit.point + (Vector3.up * 0.5f), Quaternion.identity); // send a notice to all subscribed scripts unitPlaced?.Invoke(stats); // deduct money Money.Remove(stats.price); RemoveHeldUnit(); return(true); } } return(false); }