How to check something just after something is instantiated?

Im just wondering how i could go about doing this? i need to check if the created food is hitting my tail but then i cant do it in an normal OnCollisionEnter(collision : Collision) becuse then if the tail ever hits it it gets deleted, but say if it hit it only once it first is created then it would be swell?.. Could someone please help me :smile:

#pragma strict
var food : GameObject;

var sizeX = 20;
var sizeY = 20;

function OnCollisionEnter(collision : Collision) 
{
if(collision.gameObject.tag == "Pillar")
{
Destroy (gameObject);
var position : Vector3 = Vector3(Random.Range(-sizeX, sizeX), 1.3, Random.Range(-sizeY, sizeY));
Instantiate(food, position, Quaternion.identity);
}
else if(collision.gameObject.tag == "edge")
{
Destroy (gameObject);
var position2 : Vector3 = Vector3(Random.Range(-sizeX, sizeX), 1.3, Random.Range(-sizeY, sizeY));
Instantiate(food, position2, Quaternion.identity);
}
else if(collision.gameObject.tag == "Tail")//this is the part that isent good
{
Destroy (gameObject);
var position2 : Vector3 = Vector3(Random.Range(-sizeX, sizeX), 1.3, Random.Range(-sizeY, sizeY));
Instantiate(food, position2, Quaternion.identity);
}
}
var food : Gameobject = Instantiate(food, position2, Quaternion.identity);

I usually program in c# but this should work.

Better try this ( OnCollisionStay) in place of ( OnCollisionEnter)

I dont think that this couldent do anything? that just creats food:

var food : Gameobject = Instantiate(food, position2, Quaternion.identity);

And i think this(couldent do anything) is called every frame so its not good on performance:

Bump, im still looking for this :smile:

Well, physics is calculated on the next “FixedUpdate” cycle.

You must do your own collision check either
a) before spawning the object
or
b) after spawning in the food’s Awake() or Start() method

You can use Physics.OverlapSphere do a check (if your food as a shere collider, otherwise take some other form).

It will return a list of colliders. Loop through them, if there are any and perform your checks there. Technically it’s best to do this test before spawning, as it saves most CPU cycles (you don’t have to initialize, check and derstroy the object instead you only do the check and instantiate if everything is ok).