- Home /
 
Detecting if a certain tag near another tag?
I am trying to figure how to find the distance between two prefabs that are created. Here is my current script...
var chaseDist: float = 5;
function Update () {
var player = GameObject.FindWithTag("Block"); var playerDist = Vector3.Distance(player.position, transform.position); if(playerDist <= chaseDist) { Debug.Log("Colliding"); } } Can anyone help a confused and lost kibbles? x)
I assume you have attached this script to the other tagged object that you want to see if it collides with the 'Block' tag? If so, what is the problem? The script looks fine.
Hmmmmm...it says NullReferenceException: Object reference not set to an instance of an object right when i start the game. The object is tagged "Block"....Ok, at the start of this game there are no objects with a Block tag. They are prefabs that could be created if i push a button. Could that be a reason why this isnt working?
Answer by aldonaletto · Oct 12, 2011 at 05:58 PM
You can check if player was found (and if it's a different object) before using it:
var chaseDist: float = 5;
function Update () {
 var player = GameObject.FindWithTag("Block");
 // check if some object found, and if it's not this one
 if (player && player != gameObject){
     var playerDist = Vector3.Distance(player.transform.position, transform.position);
     if(playerDist <= chaseDist)
     {
         Debug.Log("Colliding");
     }
 }
 
               }
EDITED: Things are easier if this script is in the Block creator. You can use FindGameObjectsWithTag like this:
function Update(){
    var blocks = GameObject.FindGameObjectsWithTag("Block");
    for (var player in blocks){
        var playerDist = Vector3.Distance(player.transform.position, transform.position);
        if(playerDist 
But FindGameObjectsWithTag is a slow operation (like all Find routines), and should not be called every time in Update. You can do that if your game has few objects (< 100, for instance), but it may slow down the frame rate if there are lots of objects in scene - every time the function is called, it must check each object to know if its tag is what you're looking for.
A better alternative would be to create an empty object and make it the parent of each Block right after it has been instantiated - you would have to check only their children, instead of all objects in scene:
var allBlocks: Transform; // drag the empty parent object here
var chaseDist: float = 5;
function Update () {
 for (var child : Transform in allBlocks){
     var playerDist = Vector3.Distance(child.position, transform.position);;
     if(playerDist <= chaseDist)
     {
         Debug.Log("Colliding");
     }
 }
}
And for this to work you must only add a instruction to the creation routine (in this same script!):
    ...
    // instantiate the block as before, but save its reference in a variable
    var newBlock = Instantiate(...);
    // this instruction makes the new block a child of allBlocks
    newBlock.transform.parent = allBlocks; 
    ...
                    
              Nope :( still giving me the same message. I only put the script on the prefab with the tag Block. $$anonymous$$y plan was when i spawn another one, they will both activate. But its not working. I hav no idea wat im doing wrong
Ok, we're doing bullshit here: player is a GameObject, thus player.position doesn't exist - you should write player.transform.position! I'll edit the answer to fix this error.
 Anyway, if you're trying to find the distance between two Block objects, you may have problems because FindWithTag will return the first object it encounters, which may be the same one where the script is running - the distance measured will be zero in this case! I'll include a checking for this case in the answer too.
Alright, answer fixed - and now refusing to measure anything if the the object itself is returned by FindWithTag. If there are only two objects of this kind, it will work fine: FindWithTag will return the same object for the two instances, but only the other one will report anything. If you have more than two objects, however, you'll have to change the whole thing to use FindGameObjectsWithTag - preferably attaching the script to a non Block object.
Ohhh okay. It works fine with FindWithTag. I put the script on the object that creates these objects with "Block" tags. So ins$$anonymous$$d of if (player && player != gameObject) What would i use if i changed it to FindGameObjectsWithTag? Cuz i am spawnning a bunch of objects with the same tag and i want the creator to detect them all ins$$anonymous$$d of just the first one.
Your answer
 
             Follow this Question
Related Questions
How to differentiate between prefabs on collision. 1 Answer
Prefab instead of class oClass = new class() ? 1 Answer
[C#] How can I destroy instantiated prefabs when many are created with the same name? 2 Answers
MonoBehavior Prefab modified by script in Editor loses changes when Play is hit 1 Answer
Instantiate at a position is sometimes instantiating at the wrong position 2 Answers