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 /
avatar image
0
Question by nicoolsen10 · Nov 12, 2019 at 09:24 PM · script.enemydistance

Text wont show the distance to the nearest enemy

hey idk why but my script wont tell me the distance to the nearest enemy this is my script can anyone help me?

 public void TargetFinder()
     {
         foreach (var x in enemies)
         {
             float distance = (transform.position - x.transform.position).magnitude;
 
             if (distance < currentTargetDistance)
             {
                 myTarget = x;
                 displayText.text = "My target: " + x + "\n Distance to Target: " + distance; //skal fjernes nå det er helt klart
                 currentTargetDistance = distance;
             }
         }
     }

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
Best Answer

Answer by Captain_Pineapple · Nov 13, 2019 at 02:59 PM

Hey there,

my guess is: you did never assign a proper value to currentTargetDistance. This way you never assign a text since the default value for your currentTargetDistance is 0 and you can never find a distance smaller than that.

just set the start value to float PositiveInfinity;

@yashetv : One does not need the explicit ToString() conversion here. The conversion takes place implicitly once there is a string concatenation with "+".

Comment
Add comment · Show 5 · 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 nicoolsen10 · Nov 13, 2019 at 03:16 PM 0
Share

Okay so how do i make the start valute to float positiveInfinity? i dont understand how i do that XD

avatar image Captain_Pineapple nicoolsen10 · Nov 13, 2019 at 03:35 PM 0
Share

don't want to be rude but i do not find this as "XD" as you do... i expect people who post questions here to at least first try to google their question....
I mean ist googeling "unity float infinity" so hard?

  public void Start()
  {
         yourvariable = $$anonymous$$ath.infinity;
  }


do some beginner tutorials...

avatar image nicoolsen10 Captain_Pineapple · Nov 13, 2019 at 03:58 PM 0
Share

I have done some beginner tutorials but i didnt find it cause i searched on float positiveInfinity and it came up with a vector3.positiveinfinity thats why i didnt understand it

Show more comments
avatar image
0

Answer by yashetv · Nov 12, 2019 at 11:36 PM

First of all, do you call TargetFinder in void Update() ?

And also try add "ToString()" to a "distance" and "x" value. :)


Do you have an error ?

Comment
Add comment · Show 1 · 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 nicoolsen10 · Nov 13, 2019 at 01:54 PM 0
Share

Yes its called in void Update()

And idk how to use ToString() i havnt learned that yet

and i dont get any errors

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

134 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 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 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

enemy shoots with animation 1 Answer

How to I vary animations based on proximity? 2 Answers

interact distance limits 1 Answer

I have an array of players. How can I get my enemy to go to the closest one? 0 Answers

Enemy list wont update 0 Answers


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