- Home /
Why does isTrigger = true not work without an update?
Hi guys. So I'm making a building placement tool for my rts style game. I want to set the building footprint collider to a trigger so that I can check for collisions with other buildings, scenery, etc. However it seems that this does not set the collider to a trigger.
buildingFottprint.collider.isTrigger = true;
The tick box does not change in the inspector and the trigger behavior isn't happening. I have to manually tick the isTrigger box to get it to function.
After a little poking around I did find that the tick box would change if the tool was active and updates were happening to it.
Can I only change the isTrigger variable in an update function? Does the game object need one update before allowing the change and if so can I trigger one?
Thanks a lot guys.
One of the colliders in the collision has to be a rigidbody for a trigger to work and by the sounds of it, if they are both buildings, they probably don't have rigidbodies. I'm not sure if the results will show in the editor till an update happens anyways.
This was an issue I had earlier so I gave a rigid body component to the footprint. That's not the issue here though. It starts behaving correctly only when I tick the box manually in the editor during play.
I'd disable and renable the collider to kick the physics system into knowing something has changed.
Answer by rpettefar · Mar 04, 2014 at 02:41 PM
Whydoidoit had the best solution I think. Just tried it out and works like a charm. I'll copy the comment into the answer for marking as correct
BTW the neatest way to do this might be a coroutine if a delay until physics has started is necessary:
IEnumerator Start() {
yield return new WaitForFixedUpdate();
buildingFootprint.collider.isTrigger = true;
}
Answer by Eidenai · Mar 04, 2014 at 08:57 AM
The only thing I can think of is that you're attempting to apply the change before the collider is actually initialized. Try adding a short delay.
public bool timerRunning;
public double seconds;
void Start()
{
timerRunning = true;
}
void Update()
{
if(timerRunning)
{
seconds += Time.deltaTime;
if(seconds >= 1)
{
seconds = 0;
buildingFootprint.collider.isTrigger = true;
DestroyObject(this);
}
}
}
Wow, this does seems a bit much just for turning a trigger on. I thought Unity would have a more elegant way of doing this. I'll try this and whydoidoit's suggestion later today.
The physics system doesn't really expect you to convert things between colliders and triggers. Its doing lots of work to model the scene in an efficient manner and changing things like the layer collision matrix just generally isn't what it wants you to do.
That's fair enough. Then I wonder if there is a better way to initialize my collider as a trigger programmatically then?
Have two colliders perhaps - one as a trigger and one as a collider and toggle them on and off. Having a rigidbody attached also might help as it tends to tell physics that stuff might happen - though I'm not sure it will help with this. I think you might be just fine with the disable/enable - it's probably no biggy - just not necessarily what they thought of when they coded it.
I do have the rigid body and I am not sure about having the two colliders, I only need one as a trigger. I'll be giving this a go later when I can get access to my project. Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135321im_/https://answers.unity.com/themes/thub/images/avi.jpg)