c# position check of instantiated gameobjects

I have looked up the forums and there are quite some topics regarding this. But I still have some open questions which are not really clear to me.

1).first of all is it ok to check for position constantly when I want to have two game objects interact with each other in a certain location? (performance wise) for example: the more objects I check the less performance I have etc.

2).why do my instantiated gameobjects have “(clone)” behind their name when they appear in the hierarchy?

I made a screenshot to explain my actual question:

I have two objects (instantiated game objects when pressing X) that move the the left and right of the screen. I want to check if both have a certain position.

I thoght something like “positionBallA == positionBallB” → then do something.

However if I use

float distance = Vector3.Distance (obj1.transform.position, obj2.transform.position);

unity wants me to drag two objects on the right side of the screen. But then it don´t finds the “(Clone)” objects, but only stuff which is placed in the scene already.

Anyone can help me? sorry for my bad english, its just confusing.

When instantiating an object, you can assign it to a variable in order to access it.

Check out the rockets and explosions section here Unity - Manual: Instantiating Prefabs at run time. You will have to typecast it to whatever you need. Doing so will give it a name so it won’t need to specify “clone”.

See here Remove (clone) from Instantiated Gameobjects name - Unity Answers

Checking location is troublesome and taxing if you are doing it every frame and especially for more than one object. If the location you are checking isn’t changing, then assign it to a variable. If your objects are moving around and may overlap, try using OverlapSphere to check if colliders are overlapping. Not sure if that will be what you need, but hopefully it’s a start :slight_smile: