- Home /
Check if tower is already placed
I am new to C# and am having an issue of trying to figure out how to stop another tower from being placed there once I am already done it.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEditor;
 using UnityEngine;
 
 public class TowerPlacement : MonoBehaviour
 {
 
     public GameObject _decoyTower;
     bool _placeTower;
     [SerializeField]
     GameObject[] Towers;
     PlaceTower _canPlace;
 
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hitInfo;
 
         if (Physics.Raycast(rayOrigin, out hitInfo))
         {
 
             _decoyTower.transform.position = hitInfo.point;
             if (Input.GetKeyDown(KeyCode.Alpha1))
             {
                 _decoyTower.SetActive(true);
 
             }
             if (Input.GetMouseButtonDown(1))
             {
                 _decoyTower.SetActive(false);
                 _placeTower = false;
             }
             if (Input.GetMouseButtonDown(0))
             {
                 if (hitInfo.transform.gameObject.tag == "Tower" )
                 {
                     //check hitpoint = open spot & if it is can place it there
                     Instantiate(Towers[0], hitInfo.point, Quaternion.identity);
                     //placetower. can place = false}
                     
                 }
 
             }
 
 
         }
 
     }
 }
 ![using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlaceTower : MonoBehaviour
 {
   public  ParticleSystem Tower;
     ParticleSystem.MainModule settings;
     bool _canplace = true;
     // Start is called before the first frame update
     void Start()
     {
        
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     public bool CanPlace()
     {
 
         
        
             return _canplace;
         
     }
     public void OnMouseEnter()
     {
  
         var color = Tower.main;
         if (Tower != null)
         {
             color.startColor = new Color(0,1,0,0);
         }
     }
 
     public void OnMouseExit()
     {
         var color = Tower.main;
         if (Tower != null)
         {
             color.startColor = new Color(1, 0, 0, 0);
         }
     }
 }][1]
[1]: /storage/temp/161765-capture.jpg
I am trying to figure out how to get the scripts to talk to eachother
 
                 
                capture.jpg 
                (119.7 kB) 
               
 
                
                 
                capture1.jpg 
                (199.3 kB) 
               
 
              
               Comment
              
 
               
              Answer by ShadyProductions · Jun 12, 2020 at 02:45 PM
If the place tower script is on the decoys you could do
 var placeTowerScript = hitInfo.transform.gameObject.GetComponent<PlaceTower>();
 if (placeTowerScript.CanPlace == true) 
 {
     // Instantiate                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
     placeTowerScript.CanPlace = false;
 }
Make sure you set your canplace variable to public, otherwise u won't be able to access it.
Your answer
 
 
             Follow this Question
Related Questions
RayCast Boolean Help 1 Answer
Having trouble with puase menu using standard playercontroller 0 Answers
Rotating enemy with direction 1 Answer
Slenderman like game pages not working! 2 Answers
Tilemap Collider 2D 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                