Raycasting C# to Unityscript Vector2 problem.

Hi all,

I am working on converting so C# code to Unityscript and have found a problem that i am having problem solving.

The C# is:

activePiece.transform.position = PiecePosition((Vector2)piecePositions[activePiece.name]);

And as far as i have gotten in the conversion to unityscript is:

activePiece.transform.position = PiecePosition(piecePositions[activePiece.name]);

I had to removed the Vector2 because it kept coming up with an error and would not let me test the game.

Without the Vector2 i can run the game, but as soon as i click on a puzzle piece i get the following error.

InvalidCastException: Cannot cast from source type to destination type.
JigsawPuzzle.Update () (at Assets/Scripts/JigsawPuzzle.js:324)
DemoJigsawPuzzle.Update () (at Assets/Scripts/DemoJigsawPuzzle.js:41)

The error is being called on the line 324 which is the lie of code above that i (tried to) convert, problem is i am not sure how to format the code so that i can make it work with the Vector2 all combinations that i have tried have led to errors.

A small breakdown of the code for you.

activePiece is a gameobject.
PiecePosition is a function.
piecePositions is a Hashtable.

Any help would be greatly appreciated. :slight_smile:

Thanks.

-EDIT-

C#:

// calculate right position for a x,y positioned piece on puzzle 
    private Vector3 PiecePosition(Vector3 pos)
    {
        float dX = transform.localScale.x / size.x;
        float dY = transform.localScale.y / size.y;

        // determine the position related x/y vector for this piece
        Vector3 positionVector =
            ((((transform.localScale.x / 2) * -1) + (dX * (pos.x - 1))+ (dX * (spacing/2))) * transform.right * -1) +
            (((transform.localScale.y / 2)) - (dY * (pos.y - 1)) - (dY * (spacing/2))) * transform.up;

        // set piece position to its right spot on the puzzle
        return transform.position +
            transform.forward * ((transform.localScale.z / 2) + 0.001f) +
            positionVector;
    }

And what i converted it to in unityscript:

//Calculate right position for a x.y positioned piece on puzzle.
	function PiecePosition(pos : Vector3) : Vector3
	{
		var dX : float = transform.localScale.x / size.x;
		var dY : float = transform.localScale.y / size.y;
		
		//Determine the position related x/y vector for this piece.
		var positionVector : Vector3 = ((((transform.localScale.x / 2) * -1) + (dX * (pos.x - 1)) + (dX * (spacing / 2))) * transform.right * -1) + (((transform.localScale.y / 2)) - (dY * (pos.y - 1)) - (dY * (spacing / 2))) * transform.up;
		
		//Set piece position to its right spot on the puzzle.
		return transform.position + transform.forward * ((transform.localScale.z / 2) + 0.001) + positionVector;
	}

-EDIT2-

Ohh wait you wanted piecePositions.

This is the Hashtable.

private var piecePositions = new Hashtable();

At the top of your file, put:

#pragma strict

then the compiler will tell you what is wrong, instead of leaving until runtime to give you a nasty surprise. Yes, this should be the default.

In this case, you’re actually going to run into more trouble, since you have a Hashtable of a ValueType.

Best to just convert that to a Dictionary.<String,Vector2>, and benefit from better error messages and better performance.