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 Fikzy · Apr 14, 2014 at 06:40 PM · arraysfor-loop

For loop multiplying the action by the lenght of the array.. WHY ?!

So i have a script that puts in an array every GameObjects that enter the trigger, remove them when they exit it and aplly a force to all of them when they stay in the trigger. Here is the script :

 import System.Collections.Generic;
 
 static public var inUpObjects: List.<GameObject>;
 
 function Start () {
     inUpObjects = new List.<GameObject>();
 }
 
 function Update () {
 
 }
 
 function OnTriggerEnter(other:Collider) {
     inUpObjects.Add(other.gameObject);
 }
 
 function OnTriggerStay() {
     for (var inUpObject:GameObject in inUpObjects) {
         inUpObject.rigidbody.AddForce(Vector3.down * Atmocube.gravity);
     }
 }
 
 function OnTriggerExit(other:Collider) {
     inUpObjects.Remove(other.gameObject);
 }

Okay it's working fine but when a second GameObject enter the array the force is multiplied, when a third GameObject enter the array the force is again multiplied, etc..

Any ideas WHY ?!

Comment
Add comment · Show 2
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 perchik · Apr 14, 2014 at 06:40 PM 0
Share

Perhaps you need to do something to check to see if that object is already in the List.

avatar image Fikzy · Apr 14, 2014 at 06:44 PM 0
Share

What do you mean ? Why would i have to check ? The force is a gravity force so it needs to always be active..

EDIT : I've tried : function OnTriggerEnter(other:Collider) { for (var searchedObject:GameObject in inUpObjects) { if (searchedObject != other.gameObject) { inUpObjects.Add(other.gameObject); } } } But it still increasing the force..

1 Reply

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

Answer by jonSG · Apr 14, 2014 at 07:32 PM

OnTriggerStay() gets called gets called once per frame for every collider that is touching the trigger.

2 objects in the trigger, you get 2x the force and so on if you are also itterating over the collection.

In effect, you are duplicating what the OnTrigger system is doing for you.

What if you get rid or your OnTriggerEnter and OnTriggerExit then do something similar to:

 function OnTriggerStay( Collider inUpObject ) {
     inUpObject.rigidbody.AddForce(Vector3.down * Atmocube.gravity);
 }
Comment
Add comment · Show 1 · 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
avatar image Fikzy · Apr 14, 2014 at 07:41 PM 0
Share

GOD THX ! I took like 5hours to make all of this and end up knowing that is just needed 1 line of code..

THHHHHX ! I'm so dumb..

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[C#]For-loop in For-loop crashes Unity. 1 Answer

naming a variable with a number 1 Answer

going through each item of a multi-dimensional array 1 Answer

GUI button image next 2 Answers

ios cant access variables in array 1 Answer


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