Monobehaviors, colliders, and triggers

Hi everyone,

In my program, I have a variety of game objects sitting on a plane. They’re all tagged with unique names.

The camera is top-down view.

I can “select” multiple objects by clicking and dragging my mouse around the objects (lasso effect). By clicking and dragging, the program creates a transparent 3D polygon to match the motion of the mouse. The 3D polygon has MeshCollider.IsTrigger active.

So if I have an object tagged “Cube”, and it’s inside this 3D polygon, I can use the following function to get its tag.

OnTriggerStay (col : Collider)
{
   Debug.Log(col.tag);
}

I now have “Cube” being printed in my log every update.

Similarly, if I lasso 3 objects, “Cube”, “Sphere”, “Pyramid”, I now have “Cube”, "Sphere, “Pyramid” being printed in my log every update. I would like this to only be printed once per lasso.

I cannot use:

OnTriggerEnter()
OnTriggerExit()

At no point are these objects entering or leaving this 3D polygon. Nothing gets triggered this way.

So I have to stick with:

OnTriggerStay()

I cant do the following either:

//////////////////////
//SelectionScript.js//
//////////////////////

static var allowTrigger : boolean = false;
//Gets turned on from another script (Once every time the 3D polygon mesh is updated/created)

function OnTriggerStay(col : Collider)
{
   if(allowTrigger)
   {
      Debug.Log(col.tag);
      allowTrigger = false;
   }
}

This won’t work because lets say I lasso “Cube” “Sphere”, and “Pyramid”. “allowTrigger” will get turned off after “Cube” is printed. I would need to turn it off after all 3 are printed.

Also, I have duplicate objects in my game. So at some point “Cube” “Cube” “Sphere” could be lassoed. Just a heads up in case it affects suggestions.

Any thoughts?
Alex

Regarding the issue of having the same name, you could use the Instance IDs (GetInstanceID) of each object, and not use the name.

Regarding the allowTrigger being set to false, I’m not sure I understand, but you could save all the objects (instance Ids) that are inside the lasso into an array, and flag each array item once you have processed each object selection. Then, once all array items have been processed, only then turn allowTrigger to false.

Just a couple of thoughts…

Thanks for the reply. I was thinking along the lines of the whole array ID thing as well. Just wasn’t sure if unity had something like that integrated in it or if i’d have to macguyver my own.

Before I start working on this, can you just verify if I’ve grasped your concept by going through this example:

If I had 3 game objects in the hierarchy:

Cube
Sphere
Pyramid

Lets say I lasso those 3. I’m assuming that the 3 objects will take equal turns calling OnTriggerStay() in some ordered manner.

Having said that, this should work, right?:

//SelectionScript.js//

var idArray : int[];
static var allowTrigger : boolean = false;
//allowTrigger will get turned on from another script (when the user tries to lasso something)

function OnTriggerStay (col : Collider)
{
  if (allowTrigger)
  {
     //Keeps track of duplicates
     var allowCol : boolean = true;

     //Verify that this object isnt a repeat
     for(x = 0; x < idArray.length; x++)
     {
        if(col.GetInstanceID() == idArray[x])
        {
           allowCol = false;
        }
     }

     //If the object wasn't a repeat
     if(allowCol)
     {
        //Add it to my selection
        idArray.Push(col.GetInstanceID())
     }
     //Otherwise the object was a repeat
     else
     {
        //That means all objects have been called
        allowTrigger = false;
        
        //I now have an array with an ID for all the objects that have been selected
        ContinueGame();
     }
  }
}

What do you think?

Thanks again for the reply
Alex

Works great!

Thanks again

Alex

Glad to know it works!