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 Alienjesus · Apr 26, 2011 at 05:47 PM · rigidbodytriggerloopontriggerstaymass

OnTriggerStay, add the mass of all objects inside the trigger together

Hi,

I'm using a trigger for a set of scales, where if the mass of all the objects inside the trigger is above a certain value, the scales tip. However, I can't figure out how to get add the mass of all objects inside the trigger.

At first I used OnTriggerEnter and OnTriggerExit to add or deduct the mass of each item that was added to the scales. However, due to the code I have for moving objects making the rigidbodies not detect collisions, the Enter code worked, but the Exit code was never activated, meaning the weight stayed the same even when objects were removed. This is something I'm unwilling to change.

So I tried to use OnTriggerStay instead, but I don't know how to get the mass of all the objects inside the trigger instead of just one. Here'smy current code:

function OnTriggerStay (other: Collider){
    if (other.transform.rigidbody){
    //scaleWeight = scaleWeight + other.transform.rigidbody.mass;
    scaleWeight = other.transform.rigidbody.mass;
    Debug.Log (scaleWeight);
    }
}

When I used this code with 2 objects inside the trigger (weighing 1 and 2 respectively), it only registered the object which weighed 2. I kinda knew it would do so (as there is not actual adding going on here), but I can;t figure out how to write the code to add together all objects found. Would I use some sort of loop?

The blanked out script constantly added to the total mass every frame until it reached ridiculous proportions.

Comment
Add comment · Show 1
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 Muzz 1 · Apr 26, 2011 at 05:51 PM 0
Share

What about using a GetComponent Script, getting all the rigidbody masses, and then using that?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xCRKx TyPHooN · Apr 26, 2011 at 07:02 PM

When you have an object enter the trigger, add it to a list and than total the list's mass. This way, you will have a list of objects currently inside the trigger. When they exit the trigger, simply pop them off the list. And I would use OnTriggerEnter and OnTriggerExit with the lists... it eliminates a lot of unnecessary checks you get with OnTriggerStay. I know you said you couldn't get it to work, but I don't see how you are going to manage to subtract objects that leave the trigger without an exit function...

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

No one has followed this question yet.

Related Questions

Change size and mass on collision 1 Answer

OnTriggerEnter not being called, have trigger, RB, and colliders set? 1 Answer

Simple Triggers problem. 1 Answer

How to give dynamically add mass to a gameobject from volume(collider.bounds.size) and density 1 Answer

Adding force with a trigger depends on location 0 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