• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kibbles · Oct 12, 2011 at 03:31 PM · prefab

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)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Meltdown · Oct 12, 2011 at 03:33 PM 0
Share

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.

avatar image Kibbles · Oct 12, 2011 at 05:42 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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 t$$anonymous$$s one
 if (player && player != gameObject){
     var playerDist = Vector3.Distance(player.transform.position, transform.position);
     if(playerDist <= chaseDist)
     {
         Debug.Log("Colliding");
     }
 }

}

EDITED: T$$anonymous$$ngs are easier if t$$anonymous$$s script is in the Block creator. You can use FindGameObjectsWithTag like t$$anonymous$$s:

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 c$$anonymous$$ldren, instead of all objects in scene:

var allBlocks: Transform; // drag the empty parent object here var chaseDist: float = 5;

function Update () {

 for (var c$$anonymous$$ld : Transform in allBlocks){
     var playerDist = Vector3.Distance(c$$anonymous$$ld.position, transform.position);;
     if(playerDist <= chaseDist)
     {
         Debug.Log("Colliding");
     }
 }

} And for t$$anonymous$$s to work you must only add a instruction to the creation routine (in t$$anonymous$$s same script!):

    ...
    // instantiate the block as before, but save its reference in a variable
    var newBlock = Instantiate(...);
    // t$$anonymous$$s instruction makes the new block a c$$anonymous$$ld of allBlocks
    newBlock.transform.parent = allBlocks; 
    ...
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kibbles · Oct 12, 2011 at 10:01 PM 0
Share

Nope :( still giving me the same message. I only put the script on the prefab with the tag Block. My plan was when i spawn another one, they will both activate. But its not working. I hav no idea wat im doing wrong

avatar image aldonaletto · Oct 13, 2011 at 03:17 PM 0
Share

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.

avatar image aldonaletto · Oct 13, 2011 at 03:26 PM 0
Share

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.

avatar image Kibbles · Oct 13, 2011 at 05:36 PM 0
Share

Ohhh okay. It works fine with FindWithTag. I put the script on the object that creates these objects with "Block" tags. So instead 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 instead of just the first one.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Objects flickering between dark and light 7 Answers

Script Not Working On Prefab 2 Answers

Prefab "Disappears" On Game Restart [Fixed!] 0 Answers

Script uses transform.position of Prefab instead of Instance 1 Answer

Prefabs not working properly 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges