- Home /
why is part of my script being ignored?
sorry about the title, I'm not sure how to specify it in there alone.
I have narrowed the problem down to:
if (hit.collider.gameObject.layer == "WestWallTrigger"){
Instantiate(myBullet, hit.point, Quaternion.Euler(Vector3(180, 0, 0)));
}
anything i put within those brackets just won't trigger, and I've no idea why. it doesn't matter what layer I use, I've tried several, the script just won't continue to, in this case, instantiate at the hit point. Do any of you know why?
Need a bit more script for me to be able to help;how are you getting hit for example. Is this a ray, or on collision?
Thanks.
Answer by Owen-Reynolds · Feb 17, 2013 at 03:58 PM
layer is an int (not a string.) Look up which layer WestWallTrigger is on and use that number in your ==.
May as well write the suggested debug lines in the other reply, just to show that. Are you using javascript? In C#, a compile error would pop up: "cannot convert string to int." Maybe #pragma strict in unityscript would also give that. Helps find lots of errors quickly.
I'll get right on and test that. I'm using Java, and this is practically a direct copy of what someone else used as an example. I swear if that's all that was needed... still I didn't get any error like "cannot convert string to int."...
edit: haha, yup... thanks for the quick reply, man. Just had to use the layer number.
Answer by Dave-Carlile · Feb 17, 2013 at 01:22 PM
Add a couple of Debug.Log
messages before the if
statement.
Debug.Log("Layer=" + hit.collider.gameObject.layer);
Debug.Log("Name=" + hit.collider.gameObject.name);
What does it show? Odds are the layer isn't equal to "WestWallTrigger", so the if
conditional evaluates to false, which means the code inside the brackets doesn't execute.
What is the context of this code? Have you done a Raycast
? If your log messages don't display, that means the function this code is in isn't being called, or there's a previous if
conditional that's evaluating to false.