- Home /
Collider will not enable through script
I cannot get these two scripts to work together correctly. I have this first script attached to a game object with the collider turned off.
var stumpColliderEnable;
function Start ()
{
ColliderState();
}
function ColliderState()
{
collider.enabled = stumpColliderEnable;
//stumpColliderEnable = false;
}
And this second one on my player.
var treeStumpCollider : TreeStump;
//var treeChunkCollider : TreeChunk;
//var treeTrunkCollider : TreeTrunk;
private var theCollider : String;
function OnTriggerEnter (other : Collider)
{
theCollider = other.tag;
if (theCollider == "TreeStump")
{
treeStumpCollider.stumpColliderEnable = true;
}
}
function OnTriggerExit (other : Collider)
{
theCollider = other.tag;
if (theCollider == "TreeStump")
{
treeStumpCollider.stumpColliderEnable = false;
}
}
For some reason the collider will not enable when the object enters the trigger I have on my player. I have a capsule collider attached to my player set to trigger as well as a rigidbody. Any ideas as to what I might be doing wrong are greatly appreciated.
var stumpColliderEnable;
is never used in unity, i dont know why it would compile, you have to write
stumpColliderEnable : boolean; stumpColliderEnable : boolean = true; stumpcolliderEnable = true;
or unity will not run it properly.
Second thing... is to make sure you are using Tags properly, Other.Tag looks abit fishy .
You can't have the collider turned off on the object you want to interact with the trigger. Triggers require the collider in order to work.
Triggers work by having a collider pass through it. If the collider is disabled, it will think nothing is passing through it, even if, visually it looks like it is.
What are you trying to do with your code? What is your goal by turning the collider on or off?
I have a ton of trees in my game and all of them are objects to allow for easier interactions. The problem is I have colliders and rigidbodys on all of them and when they are turned on my game slows down alot. So I am looking for a way to only turn them on when I am close.
You could probably remove the rigidbody from the trees. They shouldn't need them. Also, just have the trees with a capsule collider ins$$anonymous$$d of a mesh collider. In most cases that should be good enough.
How many trees are we talking? You should be able to have at least 100 with capsule colliders with no real issue I would think.
If you have more than 100, you should be using the terrain generator's trees ins$$anonymous$$d. That has a lot of optimization options in it that can allow you to have hundreds upon hundreds of trees.
If you have Unity Pro you can use Occlusion Culling as well. That is basically where the camera only renders what it sees. That would probably help quite a bit but it's Pro only.
The only other option I can think of is to have a box collider on all the trees in the scene then use an invisible box on the player that is a trigger. When the trigger touches a tree, it replaces the box collider with the more detailed collider.
It would be similar to what you're trying to do, but ins$$anonymous$$d of turning the collision completely off, it would just change it to a more simple, thus less resource demanding collider.
To do that it would basically be when it enters the trigger:
other.collider.gameObject.GetComponent<BoxCollider>().enabled = false;
other.collider.gameObject.GetComponent<$$anonymous$$eshCollider>().enabled = true;
Then just reverse it for when it leaves the trigger.
I actually have thousands of trees but I am using and lod system for each tree. And they all have 3 meshes as well. One for the trunk that falls and needs the rigidbody for that(unless I use animation), one for the stump thats left and eventually grows into a new tree and a small mesh in the middle that disappears as I cut it.
All three meshes have capsule colliders. I think I about have it nailed down. As far as the performance goes it is still very good with my occlusion culling, lod system and the batching I receive from using the same materials on all trees(even with thousands of trees). Plus it looks alot better and I don't have to turn the terrain trees into objects when I cut them down.
The last issue I am having is when I have all those colliders and rigidbodys enabled it kills fps. $$anonymous$$y profiler is showing physics.simulate for the fps drop and when I turn them all off it is ok. I have found a way to turn colliders on and off with script just not when I am close so that is my current state lol. Sorry if that was too much!
Answer by Lucanio · Jul 21, 2014 at 08:45 AM
You need to call treeStumpCollider.ColliderState() after changing treeStumpCollider.stumpColliderEnable
Your answer
Follow this Question
Related Questions
Script error: OnTriggerEnter 1 Answer
Can requirecomponent specify IsTrigger on a collider? 1 Answer
Can't figure out how to use multiple triggers in single scene 1 Answer
Making a Ball Respawn after collision with Goal? 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers