- Home /
how to only destroy certain blocks?
hey just wondering how i could make this code only destroy blocks that have been made not original? or to not destroy certain objects say blocker or somthing?
 var blockPrefab : GameObject;
 var hit : RaycastHit;
 var range : float = 4;
 var blockLayer : LayerMask = 1;
  
 function Update () {
     if (Input.GetMouseButtonDown(0))
         PlaceBlock();
     if (Input.GetMouseButtonDown(1))
         DestroyBlock();
 }
  
 function PlaceBlock() {
     if (HitBlock()) {
         var cube = Instantiate(blockPrefab ,hit.transform.position + hit.normal, Quaternion.identity); 
     }
 }
  
 function DestroyBlock(){
     if(HitBlock()){
         Destroy(hit.transform.gameObject);
     }
 }
  
 function HitBlock() : boolean{
     return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
 }
Answer by aaronov · May 09, 2013 at 05:27 AM
One option is to set the tag variable of the instantiated cube to something like "Blocker" then in your check for hitting a block you can check if the GameObject's tag matches. Eg.
 function HitBlock() : boolean{
     if(Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer))
     {
         if (hit.collider.gameObject.tag == "Blocker")
             return true;
     }
 
     return false;
 }
thanks that helped alot i just changed it to this function HitBlock() : boolean{ if(Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer)) { if (!hit.collider.gameObject.tag == "Blocker") return true; }
     return false;
 }
 function HitBlock1() : boolean{
     if(Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer))
 
 }
I suggest you watch this video on how to format your posts properly
Your answer
 
 
             Follow this Question
Related Questions
Problem With Enemy AI 0 Answers
My monster only choose one Target 2 Answers
Make Deaths end game 1 Answer
Vehicle help 1 Answer
if statement error 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                