- Home /
Float seen as Object, help?
Hi, I have to make a script that choose the nearest object, but it doesn't work... Here is my code:
 #pragma strict
 var Senso : Transform;
 var Fushiko : Transform;
 var Yuki : Transform;
 public var frasiFushiko = new Array("ciao","large");
 public var frasiYuki = new Array("ciao","large");
 public var frase = "";
 
 var minval : float;
 var nearest : int;
 public var characters : Array;
 var lowestIndex : int;
 
 function Start () { 
     print (frasiYuki[0]);
 }
 
 function Update () {
     var i : int;
     characters = [dist(Senso,Fushiko),dist(Senso,Yuki)];
     frase = frasiFushiko[1];
     minval = characters[0];
     for (i=0; i < characters.length; i++)
         {
         if (characters[i] < minval)
             {
             minval = characters[i];
             lowestIndex = i;
             }
     }
 }
 
 function dist (start : Transform, end : Transform){
     var cDistance : float;
     cDistance = Vector3.Distance (start.transform.position, end.transform.position);
     return cDistance;
     }
It tell me "(25,35): BCE0051: Operator '<' cannot be used with a left hand side of type 'Object' and a right hand side of type 'float'."
but cDistance is clearly a float...
I converted it to an answer so that you can accept it to mark the question as solved.
You are doing this kinda wrong. Avoid using Array and prefer the built-in versions with []. Casting is slow and cumbersome. Also, it leads to this kind of issue.
You should go for appropriate type from the start.
Answer by ExTheSea · May 01, 2013 at 11:30 AM
cDistance is but characters[i] propably isn't. Try casting charaters[i] to a float like var distance : float = characters[i]; if(distance < minval){....
I'm guessing the array returns its value as the parent class object. Another way would be to use another array like a float array var characters : float[];
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                