- Home /
Bullet triggers wrong collider
Hello again, my Bullet with the following script triggers a collider that should only trigger for the tag.player
public float bulletSpeed = 10;
public Rigidbody bullet;
void Fire()
{
Rigidbody bulletClone = (Rigidbody)Instantiate(bullet, transform.position, transform.rotation);
bulletClone.velocity = transform.forward * bulletSpeed;
}
void Update()
{
if(Input.GetButton("Fire2"))
{
if (Input.GetButtonDown("Fire1"))
Fire();
}
}
and here the collider script
private Light[] lights;
public GameObject lightParent;
void start(){
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
lights = lightParent.GetComponentsInChildren<Light> ();
foreach (Light light in lights) {
light.enabled = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
lights = lightParent.GetComponentsInChildren<Light> ();
foreach (Light light in lights) {
light.enabled = false;
}
I don't want my bullets to trigger the collider (looks like a disco lightshow). Thanks.
Answer by Oribow · May 08, 2016 at 09:46 AM
You missed the brackets {} after each of your if statements in the Trigger methods. Without them, only the first line after the if statement is considered part of it. The rest of the lines will just execute ignoring the if statement. Your IDE makes that clear to you by changing the indent of the line beneave the if statement, but not the indent level of the following lines. Pro tip: If you are using visual c# as IDE, you can auto format your code by pressing ctrl and e, then press f.
Yes, that worked, I'm still new to c#, but I am using mono, if you got any tips regarding monoDevelop please share them with me. Again thank you very much.