making position checker - is area empty or not (with trigger)?

I’m doing a ghost object, that checks is there any place for new object.
I’m having a function:


function Ghost(parent : GameObject)
{
var checker : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
checker.AddComponent ("Rigidbody");
checker.collider.isTrigger = true;
checker.transform.parent = parent.transform;
checker.transform.localPosition  =  Vector3(0,0,Distance);

need something like on collider enter inside function || that will give a true or false value on return

Destroy (checker);
}


I tried to add some script, with returning value on trigger enter… But unity returned me beginning value - its like unity don’t have a time to check trigger - he just immediately returns a beginning value…

Is there any way to do it from a function like I have, without a new scripts… All I need to add to checker gameObject, something like - if (checker.trigged) return false.

If you are ok with checking a sphere, look at the OverlapSphere function. This will return a list of all objects that intersect that given sphere. Then you just see if that is zero and place your object.

You’re almost there: just wait for the next FixedUpdate before destroying your ghost object - collisions are detected in the physics cycle. But you should disable its mesh renderer, or the ghost would appear for a while - very creepy! Another point: the Ghost function must be called in a coroutine, because you must wait its end to know the result.

Put all things together, your script could become something like this:

var somethingIsThere = false;

function WarnDaddy(){ // ghost calls this function if something detected
  somethingIsThere = true;
}

function Ghost(parent : GameObject){
  var checker : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
  checker.AddComponent(GhostScript); // you need a script in your ghost!
  checker.AddComponent(Rigidbody);
  checker.renderer.enabled = false; // you don't want the ghost appearing...
  checker.rigidbody.isKinematic = true; // avoid gravity or other physics effects
  checker.collider.isTrigger = true;
  // define rotation and position based on "parent" object...
  checker.transform.rotation = parent.transform.rotation;
  checker.transform.position = parent.transform.TransformPoint(Vector3(0,0,Distance));
  // but child the ghost to THIS object to get the result:
  checker.transform.parent = transform; 
  somethingIsThere = false; // clear flag
  yield WaitForFixedUpdate(); // wait collision detection
  Destroy(checker); // exorcise ghost
}

// How to use: call Ghost via yield, then read the somethingIsThere variable:

  yield Ghost(someObject);
  if (!somethingIsThere){
    // there's nothing in that place
  }

Ghost script (GhostScript.js):

function OnTriggerEnter(other: Collider){
  // if something is there, warn your daddy:
  transform.parent.SendMessage("WarnDaddy"); // call parent function WarnDaddy
}