:!:
unknown error happened when i tried convert c# code to javascript code, the code is to make “Instant Replay” by record an object positions then replay it again, in c# code worked well, but when i convert it to javascript it gave me error, the following are codes and console error report, I hope someone guide me where is my wrong in javascript code:
c# code:
using UnityEngine;
using System.Collections.Generic;
public sealed class Recorder : MonoBehaviour
{
private bool isRecording = false;
private bool isReplaying = false;
private int currentFrame = 0;
private List<Vector3> snapShots = new List<Vector3>();
private void FixedUpdate()
{
if (isRecording)
{
gameObject.name = "Recorder (Recording)";
snapShots.Add(transform.position);
}
else if (isReplaying)
{
gameObject.name = "Recorder (Replaying)";
transform.position = snapShots[currentFrame];
++currentFrame;
if (currentFrame >= snapShots.Count)
{
isReplaying = false;
}
}
else
{
gameObject.name = "Recorder (Idle)";
}
// Toggle recording
if (Input.GetKeyDown("1"))
{
isRecording = !isRecording;
isReplaying = false;
if (isRecording)
{
// Create new snapShots for new recording
snapShots = new List<Vector3>();
}
}
// Toggle playback
if (Input.GetKeyDown("2"))
{
isRecording = false;
isReplaying = !isReplaying;
if (isReplaying)
{
// Reset currentFrame
currentFrame = 0;
}
}
}
}
javascript code:
private var isRecording : boolean = false;
private var isReplaying : boolean = false;
private var currentFrame : int = 0;
private var snapShots = new Array (Vector3);
function FixedUpdate()
{
if (isRecording)
{
gameObject.name = "Recorder (Recording)";
snapShots.Add(transform.position);
}
else if (isReplaying)
{
gameObject.name = "Recorder (Replaying)";
//transform.position = snapShots[currentFrame];
transform.position = snapShots[currentFrame];
currentFrame++;
if (currentFrame >= snapShots.length)
{
isReplaying = false;
}
}
else
{
gameObject.name = "Recorder (Idle)";
}
// Toggle recording
if (Input.GetKeyDown("1"))
{
isRecording = !isRecording;
isReplaying = false;
if (isRecording)
{
// Create new snapShots for new recording
snapShots = new Array(Vector3);
}
}
// Toggle playback
if (Input.GetKeyDown("2"))
{
isRecording = false;
isReplaying = !isReplaying;
if (isReplaying)
{
// Reset currentFrame
currentFrame = 0;
}
}
}
console report:
InvalidCastException: Cannot cast from source type to destination type.
RecorderJS.FixedUpdate () (at Assets\scripts\RecorderJS.js:18)