Search Unity

Collision not working as expected...

Discussion in 'Scripting' started by tecra134, Jun 26, 2012.

  1. tecra134

    tecra134

    Joined:
    Mar 19, 2011
    Posts:
    94
    I have a bunch of objects working as a domino effect. For right now, I'm just trying to set a boolean for each object when it collides with the next one in line. Currently they all share the same name because they are instantiated via prefab. For some reason only the second object and the second to last object are the only two that are picking up on the collision. This is how I have the code setup now.
    Code (csharp):
    1.  
    2. function OnCollisionEnter(other)
    3. {
    4. if(other.gameObject == GameObject.Find("ObjectName(Clone)"))
    5. {
    6. hasCollided = true;
    7. }
    8. }
    9.  
    Again. For some reason, only the second object and the second to last object are the only two that are having that boolean changed. It doesn't seem to matter how many I have in the row, it's always the second and second to last that seem to pick up on the collision.

    Any idea on why this is happening?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    GameObject.Find just returns the first object in the scene that it finds with that name. There's no particular reason why it should happen to be the same one that the object collided with. Maybe you should be using the name or tag.

    --Eric
     
  3. tecra134

    tecra134

    Joined:
    Mar 19, 2011
    Posts:
    94
    I did try using GameObject.FindWithTag but that returned the same results. If all the same objects are utilizing the same script on each object, I would think it wouldn't matter if they're all named the same. The script is only waiting for an object of that name to collide with it.

    I can see it it being an issue if I was say using a single script that is trying to call all the GameObjects of that name. Then I would except it to only call the first on on the list.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Code (csharp):
    1.  
    2. if (other.gameObject.name == "ObjectName(Clone)")
    3.     hasCollided = true;
    4.  
    Unless I'm misunderstanding what you're trying to do...
     
  5. tecra134

    tecra134

    Joined:
    Mar 19, 2011
    Posts:
    94
    That did it... Thanks! I need to learn to scroll down in the API... :D