Hi guys. I wondered if someone could help me out with a small issue I have with OnTriggerEnter. The scenario is this; I have a barrel that the player can light up by touching the top of the barrel with a flame. The barrel has a script that looks for object tagged flame and ones it comes into contact via the sphere colliders (which are switched to triggers) all the elements that makes up the fire on the barrel is switched on. The weird part is that it worked for a little while (5min or so) then I adjusted the sphere colliders (yes they are still touching:) )a little and it suddenly stopped working. It’s probably just a stupid small mistake, but I’ve gone through everything and I just can’t see where the mistake is. Btw here’s the script:
It honestly doesn’t look like a script error. I don’t know how your scene is setup, but it looks a bit complicated. Personally, I would make a capsule collider that encompasses the barrel, then ask if the player is there, and if he is, ask if the user has pressed a key to make the lighting work.
Oh, and use OnTriggerStay for that. Not for what you have.
Thanks for the suggestion:) I were going for something similar as you would find in the Zelda series, where you just bring a fire source to a torch to light it up. It seems like there’s something wrong with my scene. I tried everything you suggested, but still it’s a no go:/ I have no clue what’s going on.
Not sure about your problem, but it would be a lot smarter to find all your emitter components in Start() or Awake() and then just turn them on when you enter the trigger.
I’m guessing the barrel has the smoke, fire etc. as children so for instance:
var pEmitters : ParticleEmitter[];
function Start () {
pEmitters = gameObject.GetComponentsInChildren(ParticleEmitter);
}
function OnTriggerEnter () {
for (var emitter : ParticleEmitter in pEmitters ) {
emitter.emit = true;
}
}
Also, you know the tag of the other gameObject just with other.tag or other.gameObject.tag. No need to use CompareTag.
@BigMisterB, why would you use OnTriggerStay? And in your example you’re not even checking for “Flame” anymore and where did “space” come from?
First off, thanks a lot for the replies and your solution Twiik (takker å bukker å springer omkring;) ), it’s a lot more versatile script and works much better, the only thing about the script is I get a error message, thought it doesn’t stop it from working:
InvalidCastException: Cannot cast from source type to destination type.
FireOnTrigger.Start ()