Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Accessing another gameobject's collision?

Discussion in 'Scripting' started by bigkahuna, Sep 29, 2006.

  1. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    I want to trigger a camera script when one object collides with another. The object is parent to the camera and a number of other "sub-objects". I tried using a global variable like this:

    object script:
    Code (csharp):
    1. static var objectCol = false;
    2.  
    3. function OnCollisionEnter (col : Collision) {
    4.    if (col.relativeVelocity.magnitude > collisionThreshold) {
    5.         objectCol = true;
    6. }
    7. }
    camera script:
    Code (csharp):
    1. function Update() {
    2.     if (objectScript.objectCol == true) {
    3.             doSomethingHere;
    4. }
    5. }
    But that doesn't work. What I'd like to do is to check for an object collision from the camera script, is that possible? Something like this pseudo code camera script:
    Code (csharp):
    1. function Update() {
    2.     if (objectScript.objectCol.col.relativeVelocity.magnitude > objectScript.objectCol.collisionThreshold) {
    3.         doSomethingHere;
    4. }
    5. }
    The two other alternatives I'm thinking about doing are:

    1) to change the collider to the camera and make it the parent to the object, or
    2) put all the code for the camera script into the object script, since I can access the camera transforms from another object.

    Any other ideas? Thanks!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Seems to me like it ought to work...though you should put this in objectScript:

    Code (csharp):
    1.  
    2. function Start () {
    3.     objectCol = false;
    4. }
    5.  
    Because static variables don't get reset automatically (i.e., once set to a value, it will keep that value even if you stop and run the scene again, unless you specifically set it otherwise). If that doesn't fix it, are you sure objectCol is in fact being set to true?

    --Eric
     
  3. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Thanks Eric, I missed that. But even without being re-set, for some reason it's not picking up the variable.

    Yeah, the script -should- work, but for some reason it doesn't. Still digging around to figure out why...

    EDIT: Got it! It was a stupid little typo that caused the problem. I had a bracket in the wrong spot which prevented the variable from being updated. Duh!