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 Nick4 · Jan 25, 2014 at 07:41 AM · colliderlistontriggerenter

Can't add colliding game objects to a list.

 public class Surroundings : MonoBehaviour {
     
     List <GameObject> collidedWith = new List<GameObject>();
     
     void OnTriggerEnter(Collider col)
     {
         collidedWith.Add(col.gameObject);
     }
     
     void OnTriggerExit(Collider col)
     {
         collidedWith.Remove(col.gameObject);
     }
     
     public List<GameObject> GetArray()
     {
         return collidedWith;
     }
     
 }

This is my code on each "Stone" objects in the scene. This is supposed to return a list of colliding objects.

 public class toBeDestroyed : MonoBehaviour
 {
     GameObject[] allStones;
 
     void Permission(bool perm)
     {
         if (perm)
         {
             allStones = GameObject.FindGameObjectsWithTag("Stone");
             Begin();
         }
     }
 
     void Begin()
     {
         foreach (GameObject go in allStones)
         {
             print("Prints this.");
             foreach (GameObject go2 in go.GetComponent<Surroundings>().GetArray())
             {
                 print("Doesn't print this.");
             }
         }
     }
 }

And this script has to check that array of colliders. As you can see, second foreach is trying to access that list which isn't happening. I tried to print "go.GetComponent().GetArray().Count" and it printed out 0 although those go objects sure had colliding objects.

All objects in my scene have rigid bodies with "is kinematic" enabled, collision detection using continuous and they have colliders with "is trigger" enabled. I used to return those values with no problem before. I haven't checked my project in a few days and when I was back, bam : problem.

So what could be the problem? Thanks in advance.

Comment
Add comment · Show 20
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 Benproductions1 · Jan 25, 2014 at 07:43 AM 0
Share

You're using OnTriggerEnter and OnTriggerExit, and yet talk about colliding. Are you sure you know what those are supposed to do?

avatar image Nick4 · Jan 25, 2014 at 07:53 AM 0
Share

I know, I worked with them before. They detect collisions. Why?

avatar image Benproductions1 · Jan 25, 2014 at 08:01 AM 0
Share

Actually, they detect when a collider enters the area of another collider marked with isTrigger. It doesn't detect a collision, as no collision occurs.

avatar image getyour411 · Jan 25, 2014 at 08:08 AM 0
Share

In this

  public List<GameObject> GetArray()
 {
 return collidedWith;

before the return put the Count to debug.log. Do you have to size an Array before you can start putting things into it? allStones = GameObject.FindGameObjectsWithTag("Stone");

avatar image Nick4 · Jan 25, 2014 at 08:17 AM 0
Share

getyour411 : Now that's weird, too. It says 0 again. Stones are generated randomly so I can't size the array before I assign all stones into it.

Benproductions1 : Sorry, you must be mistaken. It is OnCollisionEnter that only detects and gives detailed information on enter. OnTriggerEnter is used to simply detect collisions along with the ability to detect collisions that are already colliding with the object from the beginning. So no problem here.

Show more comments

1 Reply

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

Answer by aeroson · Jan 25, 2014 at 10:25 AM

When exactly are you calling the Permission function ? Are you sure its being called after first FixedUpdate ? (if you call it before first physics update the triggers have not yet collided)

"Start is called on the frame when a script is enabled just before any of the Update methods is called the first time."

Try to do something like this instead.

 int fixedUpdatesCounter=0;
 void FixedUpdate() {
     fixedUpdatesCounter++;
     if(fixedUpdatesCounter==2) {
         Debug.Log("First fixed update done");
     }
 }

Or

 void Start() {
     StartCoroutine("DelayedStart");
 }    
 IEnumerator DelayedStart() {
      yield return new WaitForFixedUpdate();
     Debug.Log("First fixed update done");
 }
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 Nick4 · Jan 25, 2014 at 10:29 AM 0
Share

I was about to tell you to copy and paste it as an answer. That worked great. Thanks a lot :)

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

21 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

Related Questions

Accessing a collider's collisions without OnTriggerEnter or OnTriggerStay 1 Answer

A node in a childnode? 1 Answer

OnTriggerEnter not recognising collision ?(Solved) 3 Answers

OnTriggerEnter - Hit Only One Collider 2 Answers

Trigger and Collider Issues 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