- Home /
String split script
I'm using this script to draw wireframe. Script read from string file, split coordinate and make vector3 array. In desktop mode all ok, but now in iOS give me error:
Assets/Script/Wireframe.js(33,23): BCE0050: Operator '-' cannot be used with an expression of type 'Object'.
import System.IO;
var Filename : String;
var lineMaterial : Material;
var lineColor : Color;
var lineAlpha : float;
var lineThickness = 1.0;
var line3D = false;
private var line : VectorLine;
private var LinesArray : Vector3[];
private var PointsList : Array;
private var Lines : Array;
private var i : int;
function Start () {
var sr = new StreamReader(Application.dataPath + "/" + Filename);
var fileContents = sr.ReadToEnd();
sr.Close();
var Points = fileContents.Split(","[0]);
//PointsList = new Array();
for (i = 0; i < Points.length; i++){
PointsList.Add(float.Parse(Points[i]));
}
//Lines = new Array();
for (i = 0; i < PointsList.length / 3; i++){
Lines.Add(Vector3(-PointsList[i*3],PointsList[i*3+2],-PointsList[i*3+1]));
}
LinesArray = Lines.ToBuiltin(Vector3);
line = new VectorLine("Line", LinesArray, lineColor, lineMaterial, lineThickness);
VectorManager.useDraw3D = line3D;
line.joins = Joins.Weld;
VectorManager.ObjectSetup (gameObject, line, Visibility.Dynamic, Brightness.None);
}
function LateUpdate (){
line.vectorObject.renderer.material.color.a = lineAlpha;
}
Somebody help me to correct script?
Answer by Wolfram · Jun 02, 2012 at 02:25 PM
The "Array" type is an untyped collection of arbitrary objects, so although you put floats in, the compiler doesn't know that when you're accessing the elements again.
I'm not a Master of UnityScript, since I always use C#, so I don't know what the best alternative is. But your problem should be solved by replacing every "PointsList[..." in your line starting with "Lines.Add" with "(float)PointsList[...", or maybe "float(PointsList[...])". In C# you could do "PointsList[...] as float", but I don't know if that's available in UnityScript.
Lines.Add(Vector3(-PointsList[i*3],PointsList[i*3+2],-PointsList[i*3+1]));
The best alternative is a generic List; the JS Array class should not be used even in JS.
Your answer
Follow this Question
Related Questions
Extract number from string? 3 Answers
how can i split a word to individual letters..?? 2 Answers
StringSplitOptions.RemoveEmptyEntries - Unknown identifier 2 Answers
issues splitting strings 1 Answer