Problem in finding the distance

Hi all,

I have a problem in finding the distance between some dynamically instantiated game objects and a constant object which is moving towards them. i don’t know how should I convert the gameObject to it’s position component in order to be able to use the position information inside the

Vector3.Distance(transform.position, OtherGameObject.position);

block.

Here’s a section of the codes i’m using:

for dynamic object spawner:

var clones : GameObject = Instantiate(Fence, transform.position, transform.rotation);
nextFire = Time.time + fireRate + Random.Range(-1 * fireRateTol , fireRateTol);

this created the dynamic objects. I obviously filtered useless parts of the code.

This is for dynamic objects (Fence) when created:

transform.Translate(0, 0, scrollSpeed * Time.deltaTime);

And this is the code i use in the main object to find the distance (which is not working):

var Fence : GameObject = GameObject.FindWithTag ("Fence");
				
var dist = Vector3.Distance(transform.position, Fence.position);
print(dist);

debugger says that position is not a member of unity game object or something that i don’t understand. I truly appreciate any help.

:roll:

You still have to use the transform of the GameObject, i.e. GameObject.transform to access the position. The GameObject itself does not have a position only the transform does.

Vector3.Distance(transform.position, OtherGameObject.transform.position);

So in your example it would be

var Fence : GameObject = GameObject.FindWithTag ("Fence");
            
var dist = Vector3.Distance(transform.position, Fence.transform.position);
print(dist);

:smile:

Many thanks. kiss