- Home /
GetComponent, gameObject.tag vs gameObject.layer
Im currently using a raycast system to detect touches on game objects. I cant figure out why I can grab a component from an object if I reference the object by the tag, but I cant if I reference by the layer.
This works :
 if(hit.collider.gameObject.tag == "object_controller") 
 { 
      var objScript : object_controller = hit.collider.gameObject.GetComponent(object_controller);    
     objScript.Kill();    
 }
This does not :
 if(hit.collider.gameObject.layer == 9) 
 { 
      var objScript : object_controller = hit.collider.gameObject.GetComponent(object_controller);    
     objScript.Kill();    
 }
Can anyone enlighten me?
Are you positive that 9 is the correct layer number? You can double check by using name to layer and logging it to the console
 var mask=Layer$$anonymous$$ask.NameToLayer("$$anonymous$$yLayer");
 Debug.Log(mask);
Thanks, you guys are right! Why is it that even though I physically set the layer on the prefab to 9 in the inspector, the object is on a different layer?
does collider is on object with correct layer? or collider and object with layer are different (parent and child?)
Answer by ScroodgeM · Aug 16, 2012 at 11:37 AM
modify your layer check to this (below) and methinks you'll got the answer 8)
if(hit.collider.gameObject.layer == 9) 
{ 
    var objScript : object_controller = hit.collider.gameObject.GetComponent(object_controller);  
    objScript.Kill();    
}
else
{
    Debug.Log("my layer is: " + hit.collider.gameObject.layer.ToString());
}
 
              Your answer
 
 
             Follow this Question
Related Questions
Unity Raycasting a tag rather than layer 1 Answer
Why isn't this raycast working? 2 Answers
Raycast on touch 3 Answers
WorldToViewportPoint based on transform.tag 0 Answers
Ignore Raycast Layer invisible in editor 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                