Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jaqal · Jul 21, 2014 at 04:57 AM · collidertrigger

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image drudiverse · Jul 21, 2014 at 05:02 AM 0
Share

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 .

avatar image drex150 · Jul 21, 2014 at 05:55 AM 0
Share

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?

avatar image Jaqal · Jul 21, 2014 at 06:10 AM 0
Share

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.

avatar image drex150 · Jul 21, 2014 at 06:44 AM 0
Share

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.

avatar image Jaqal · Jul 21, 2014 at 07:11 AM 0
Share

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!

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Lucanio · Jul 21, 2014 at 08:45 AM

You need to call treeStumpCollider.ColliderState() after changing treeStumpCollider.stumpColliderEnable

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges