- Home /
Changing layer of Game Objects
I have an empty game object with children(enemies). When the player attacks the layer f the enemies is supposed to change so the blade can go through the enemy. I thought this was supposed to work (the script is attacked to the weapon).
var theEnemy : GameObject;
function Attack ()
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*.5, Screen.height*.5, 0));
if (Physics.Raycast (ray, hit, maxDistance) && hit.collider.tag == "Enemy")
{
theEnemy.child.gameObject.layer = LayerMask.NameToLayer("DrawAlways");
}
}
Why do you have "child" standing there? It's not actually a variable in the unity API of a GameObject.
I saw it here http://answers.unity3d.com/questions/168084/change-layer-of-child.html
I'm realizing trying to use that was extremely stupid of me. Do you have a solution?
facepalm
Well, theres a function called "GetChild" (http://docs.unity3d.com/Documentation/ScriptReference/Transform.GetChild.html) which is very useful, and hopefully will achieve what you are looking for.
I tried to fix it and it said
$$anonymous$$ identifier: 'ChangeLayersRecursively'.
How can I change the layer? This says layer is not a member of int.
function ChangeLayer()
{
for(var i:int=0;i<transform.childCount;i++)
{
if(transform.GetChild(i).name=="Enemy")
{
i.layer = Layer$$anonymous$$ask.NameToLayer("DrawAlways");
}
}
}
Answer by nesis · Jan 15, 2014 at 05:23 AM
To answer the first post, I'll first suggest an easier way to do what you're wanting. You're currently checking hit.collider.tag
to see if you've hit the enemy. However, if you've got several enemies, a better way to check physics-related stuff is by using layers. You can create a new layer (select a GameObject, then under the Inspector tab, click the Layer dropdown, then click Add Layer..., then type in a new layer name for one of the User Layers), then select your enemy(s) in the Editor and change their layer to the one you just created.
//var theEnemy : GameObject;
//the above line isn't needed, Physics.Raycast will give
//"hit" a reference to any GameObject it hits, using
//hit.collider.gameObject. Just be careful to put your
//enemies on the same layer as layerMask, and they'll
//be able to be hit with the Raycast.
var layerMask : LayerMask; //in the Editor's Inspector tab, you can set this to a custom layer that you put your enemy(s) on
function Attack ()
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*.5, Screen.height*.5, 0));
//might not need to check hit.collider.tag, unless you've got
//multiple kinds of enemies on the same layer, in which case
//tags (and other attributes) can sometimes be handy to differentiate
if (Physics.Raycast (ray, hit, maxDistance, 1<<layerMask) && hit.collider.tag == "Enemy")
{
hit.collider.gameObject.layer = LayerMask.NameToLayer("DrawAlways");
}
}
Your answer
Follow this Question
Related Questions
Problem when GameObject and its child have different layers. 4 Answers
How do I check to see if animation layer is playing with hash id on Unity3d 0 Answers
Is there a way to mask out a sprite layer 0 Answers
Physics linecast ignoring layers 0 Answers
I need my GameObject to Spawn in front of my sprites 2 Answers