Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by simulism · May 28, 2016 at 05:10 AM · transform.positiondistance check

Determine distance to player from prefab

I am trying to make is so my player can only interact with a prefab if it is within a certain distance. I have tried several ways. When I try to set it manually by using a public variable, the data is lost when I save the new prefab and when I use:

 float dist = Vector3.Distance(GameObject.FindGameObjectsWithTag("Player").position, transform.position);
             Debug.Log("Distance to other: " + dist);
             if (dist < 1)
             {

It just does not work.. I am not sure how to set my player as the variable at run time.. thank you for your help.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by JedBeryll · May 28, 2016 at 10:08 AM

GameObject.FindGameObjectsWithTag("Player").position this returns a GameObject array so you cant access a position variable. if you do it like this: GameObject.FindGameObjectWithTag("Player").transform.position, it should work. Difference is Object not ObjectS and you put the .transform after it.

Comment
Add comment · Show 3 · 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 oyundanoyunad123 · Aug 14, 2019 at 02:00 PM 0
Share

I'm sorrry to revive the thread but when I type .transform It says that GameObject[] does not contain a definition for transform. can you please help?

avatar image JedBeryll oyundanoyunad123 · Aug 14, 2019 at 02:59 PM 0
Share
 GameObject.FindGameObjectWithTag("Player").transform.position;
 GameObject.FindGameObjectsWithTag("Player").transform.position;

Take a closer look at these 2 lines. The first returns just one GameObject, which has a transform. The other one returns an array of GameObjects, and an array does not have a transform. So you're probably using the second line, but you need the first.

avatar image oyundanoyunad123 JedBeryll · Aug 14, 2019 at 08:28 PM 0
Share

Thank you! This has been bugging me for a while.

avatar image
0

Answer by I_Am_Err00r · Aug 14, 2019 at 02:33 PM

GameObject[] means an array (or list) of GameObjects; the [] means which one in that array (or list) you want to select (0 being the first element), so for example:

  1. I have 10 enemies in a scene, I store all those in an array (or list) like this (array example here):

      GameObject[] enemies = new GameObject[10];
    
    
  2. In this example, I made an array variable of the 10 enmies into the 'enemies' variable; now lets say I want to access enemy 4, I would type something like this:

      enemies[3].transform.positoin
    
    

To get the fourth enemy's position (remember, in lists and arrays, 0 is the first element).


I hope my example of arrays and lists helped you understand the logic behind them moving forward; this example the OP is trying to measure the distance from an enemy to the Player, is that what you want? If so, this video has a great explanation on how to find the closest enemy to a player (and you can use this tutorial to do other things once you understand how to calculate distance between a list of objects and one singular object), and would direct you there to get a better understanding.


Now let's say you do in fact want to find just one GameObject, and not a list or array and you are getting that error looking for GameObject[], then instead of this code:

 GameObject.FindObjectsWithTag("Whatever").transform

make sure it isn't FindObjects, but this:

 GameObject.FindObjectWithTag("Whatever").transform

And that will return the first instance of "whatever"; so if there is only one in a scene, it will return only that one and will work for you.

Comment
Add comment · Show 2 · 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 oyundanoyunad123 · Aug 14, 2019 at 08:30 PM 0
Share

Omg this is amazing! Thank you for linking the video! It definitely will help me a lot!

avatar image I_Am_Err00r oyundanoyunad123 · Aug 14, 2019 at 08:52 PM 0
Share

Anytime! You don't have to give points for writing such a complete solution (to a 3 + year old question $$anonymous$$d you), but it would be a lot cooler if you did!

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.

Follow this Question

Answers Answers and Comments

60 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to calculate distance traveled given time and speed 0 Answers

Animator Position VS Script Vector 3 0 Answers

Make object stick to ground (For IK) 0 Answers

Missing Reference Exception 1 Answer

Changing Y position of transform position has no effect. 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