I know this is quite a cliche and probably simple question, but I am having some problems. I have an exploding barrel and when I shoot it, it explodes. I have set the barrels to explode once. No Problem. However, when there is more than one, only one of the barrels blow up and then they all switch off. I think this is because they are all sharing the same script and variables. How do I stop this or change it? Thanks.
1 Answer
1GameObject.SendMessage sends the message to ALL of the scripts of that type, hence why all of your barrels explode at the same time. Instead, you should use something like:
function OnTriggerEnter(Collider c) {// when the bullet hits the barrel...
c.transform.GetComponent(BarrelExplosion).Explode();// ...explode the barrel
}
EDIT: I didn’t see the extra code you’d posted because it was hidden, but you can also use my code snippet using the “collider” variable you get from your raycast instead of from the collider trigger I used
Yeah, I'd do this as well. It's also a lot faster than doing a SendMessage. If you have to use SendMessage then just send it to the GameObject you already have a reference to!
– iwaldrop
You're probably using static variables where you shouldn't be - but that's just a guess. Post code please. Thanks for updating the spelling errors. Still waiting on the code. =)
– DracoratThanks, I didn't notice my spelling haha. This is the code I have on the barrels... var Normal:Mesh; var Broken:Mesh; var on:boolean = true; function Start() { on = true; GetComponent("Detonator").enabled = false; } function Explode() { GetComponent("Detonator").enabled = true; var BrokenMesh:Mesh = Instantiate(Broken); GetComponent(MeshFilter).mesh = BrokenMesh; on = false; } And I use the SendMessage on my shooting script to activate the Explode function.
– camfoxWe either need to see more of the script, or you need to tell us if the variable 'on' is static or not.
– iwaldropOn isn't static - it's above the code block in the text. (He didn't quite do the formatting right) But we need to see more of the script I agree - especially that part with SendMessage.
– DracoratAhh...right. :)
– iwaldrop