- Home /
private Vector3 function?
Hi all,
I have this slice of code in C# that i am trying to convert to js and i am having not much luck.
private Vector3 PiecePosition(Vector3 pos)
{
}
I have tried to convert it into a function in js like this.
private function PiecePosition (pos : Vector3) : Vector3
{
}
But now i have two other pieces of code that are throwing up errors and i think it might be because of that function i created.
This line of code has not been changed from the C# script.
activePiece.transform.position = PiecePosition((Vector2)piecePositions[activePiece.name]);
This line of code has been changed a bit.
C#
Vector3 dV = PiecePosition((Vector2)piecePositions[activePiece.name]) - activePiece.transform.position;
js
var dV : Vector3 = PiecePosition((Vector2), piecePositions[activePiece.name]) - activePiece.transform.position;
The error i am getting on the two lines of code is:
The best overload for the method 'JigsawPuzzle.PiecePosition(UnityEngine.Vector3)' is not compatible with the argument list '(System.Type, Object)'.
I think the problem lies in the code at the very top but i don't know how to fix it.
Any help would be appreciated.
Thanks.
Because that is the language i understand the most (Unityscript). Still have heaps to learn though. :)
Answer by aldonaletto · Aug 02, 2011 at 06:10 PM
The function declaration is correct. The problem is the type casting in the problematic line. You should use:
var dv: Vector3 = PiecePosition(piecePositions[activePiece.name] as Vector2) - activePiece.transform.position;The PiecePosition function requires a Vector3 as argument, but I suppose the piecePositions is an array of Vector2, and Unity will extend it to Vector3 automatically.
Yeah you code worked well, except it through up an error at the 'as Vector2' it didn't like the 'as' part so i changed it to:
var dv: Vector3 = PiecePosition(piecePositions[activePiece.name] || Vector2) - activePiece.transform.position;
Thanks for the help.
Answer by Meltdown · Aug 02, 2011 at 06:09 PM
There are some problems with this line of code...
var dV : Vector3 = PiecePosition((Vector2), piecePositions[activePiece.name]) - activePiece.transform.position;
Firstly, your method PiecePosition, takes a single Vector 3 as an argument. You are passing your method a Vector2, then an array of piecePositions. So you are giving it two arguments, not one, and both arguments are of the incorrect type.
Secondly, you have no closing bracket at the end of the statement.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Why inpector keeps methods assigned when changed from public to private 2 Answers
Need help converting js to C# 4 Answers
C# Return Type Error? 1 Answer
calling c# function from a js file.. 1 Answer