- Home /
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;
}
}
}
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 "+".
Okay so how do i make the start valute to float positiveInfinity? i dont understand how i do that XD
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...
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
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 ?
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
Follow this Question
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