- Home /
How to enable the box collider tick box on a trigger event
Hi there,
I am unable to find a solution to this myself so I was hoping I could get a answer here.
Seen as when you turn Is Trigger off you are unable to walk through the Cubes any more, Is there a way to access the Inspector panel via scripting? Does anyone know how I could enable the Box Collider of the Cubes after I collide with another object?
I'm not sure exactly how to work this so I'm having trouble finding a solution, hope this Makes sense
Matthew
@mcfetrmatt You can set your colliders isTrigger variable to true or false from script. http://unity3d.com/support/documentation/ScriptReference/Collider-isTrigger.html>isTrigger Ref. You will need a way to reference the other cube if you want to switch it from anothers isTrigger. So setup a var for the cube you want to switch the isTrigger on or off/ or collider enabled/disabled.
Answer by aldonaletto · May 23, 2012 at 11:16 PM
You can set a collider to trigger via script with the property collider.isTrigger: just set it to true (trigger) of false (solid collider).
collider.isTrigger = true; // convert collider in trigger
collider.isTrigger = false; // convert back to collider
EDITED: Ok, so you have two box triggers: trigger 1 activates the particle emitters, but only when the player has passed the trigger 2, right? If this is correct, I think you should use a boolean flag set by trigger 2 that enables the particle emitters in trigger 1. A simple method is to call a function in trigger 1 when the player enters trigger 2: this function sets the boolean variable that enables the particles in trigger 1 - like this:
Trigger 2 script:
var trigger1: GameObject; // drag trigger1 object here
function OnTriggerEnter(other: Collider){ // call EnableTrigger(true) in trigger 1 trigger1.SendMessage("EnableTrigger", true); }
Trigger 1 script:
var fountain: GameObject; var splash: GameObject; var enable: boolean = false; // boolean flag: enable trigger 1 when true
function Awake(){ fountain.particleEmitter.emit = false; splash.particleEmitter.emit = false; }
// function called by trigger2 via SendMessage:
function EnableTrigger(onOff: boolean){ enable = onOff; }
function OnTriggerEnter(trigger: Collider){ if (enable){ fountain.particleEmitter.emit = true; splash.particleEmitter.emit = true; } }
function OnTriggerExit(trigger:Collider){ if (enable){ yield WaitForSeconds(2); fountain.particleEmitter.emit = false; yield WaitForSeconds(2.5); splash.particleEmitter.emit = false; } }
@hijinxbassist @Aldo Naletto Thanks for the advice! I tried using: collider.enabled = false; because I just want to enable the main box collider. However no errors occur but my fountains do not start emitting particles.
The scene works, but I want to effectively hide the colliders and only have them appear once I hit a specific object. So you can walk over a fountain and it wont emit but after the object is hit they will emit after that event(or well the box colliders will be able to be interacted with at least).
Setting a collider's isTrigger property doesn't affect anything but the collider behaviour. If the object has a particle effect, you must enable it when the trigger is entered. With the script below attached to the trigger, you can drag the fountain particles effect to the Fountain field in the Inspector:
var fountain: ParticleEmitter; // drag the fountain here
function OnTriggerEnter(other: Collider){ fountain.emit = true; // start emitting when trigger entered }
function OnTriggerExit(other: Collider){ fountain.emit = false; // stop fountain when trigger exited } NOTE: This works for the old particles; for the new Shuriken particles, the variable fountain should be declared ParticleSystem and fountain.Play() should be used to start emitting.
$$anonymous$$y scene is say two game objects, two cubes. On of them is a box collider for the fountain particle system to start emitting. I only want the box collider for the particle system to start after I have collided with the 2nd game object. So even though you can walk through the trigger box at any time, I only want it to activate the fountain once you collide with it AFTER colliding with the 2nd box.
Here is my code that is attached to the Cube:
var fountain: GameObject; var splash: GameObject;
function Awake(){ fountain.particleEmitter.emit = false; splash.particleEmitter.emit = false; }
function OnTriggerEnter(trigger: Collider){ fountain.particleEmitter.emit = true; splash.particleEmitter.emit = true; }
function OnTriggerExit(trigger:Collider){ yield WaitForSeconds(2); fountain.particleEmitter.emit = false; yield WaitForSeconds(2.5); splash.particleEmitter.emit = false; } Sorry I'm not sure if I have been clear enough in my intentions :) So is it possible to access the box collider part rather than the is trigger part? I'm not sure where I would put this either... Any idea of where I should look?
I edited my question to include this trigger2-enabling-trigger1 behaviour.
@aldonaletto You beauty! Thanks for all your help, it works perfectly! I had to adapt it a little to my scene as I have multiple instances but it works exactly how I wanted it to.
Thank you!
Answer by dancinoman · Feb 25, 2016 at 08:25 AM
Newer version: You have to Get your component that has the same name on the game object inspector. Like this:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
private PolygonCollider collider; //The exact collider Component name
void Start()
{
collider = GetComponent<PolygonCollider> ();
}
void Update ()
{
collider.isTrigger = false;
}