OnCollisionStay Problem & Needs Help
Hello im trying to create a building mechanism to my game. The building mechanism is done and works fine. But i've got an problem when i try to place the "building". I have islands that have meshcolliders and a script attached to a cube thats trying to detect if the cube is colliding with a collider that is called "islands". My mesh collider is named islands and has a non convex meshcollider on it. but the cube is not detecting it. Any idea why?
Here is the script that has the problem:
var buildControllerScript : BuildController;
function Start ()
{
buildControllerScript = GameObject.Find("Main Camera").GetComponent(BuildController);
}
function OnCollisionStay (col : Collision)
{
if(col.gameObject.name == "islands")
{
buildControllerScript.canPlace = true;
}
}
function OnCollisionExit (col : Collision)
{
if(col.gameObject.name == "islands")
{
buildControllerScript.canPlace = false;
}
}
Answer by Happy-Zomby · Apr 05, 2016 at 11:12 AM
Hi, on collision is linked to rigid bodies/colliders... I believe you should use On trigger stay http://docs.unity3d.com/ScriptReference/Collider.OnTriggerStay.html make sure your building has a collider with trigger turned on. Code remains the same just change oncollision to ontrigger and see if that works.
as a side note I would recommend using "tag" rather than "name" (don't ask me why :)
hope that helps,
Yeha thanks, i tried it before but the reason why i dont want to use it is because then the collider hasto be a trigger, and there is no collider that fits my "island" so thats why im using onCollision. it gets more accurate and its just better
Hi, 1. I haven't used colliders and triggers since unity 5 is out but can't you make the island collider the trigger and put the script on the island and send the no build from the island to the cube script. 2. What I did in the past in a vaguely similar situation is create a child collider to your cube with a trigger and then deactivate or destroy that child once you have placed your building. Or I guess you could remove the trigger by code.
Your answer
![](https://koobas.hobune.stream/wayback/20220612074221im_/https://answers.unity.com/themes/thub/images/avi.jpg)