ERROR ! Input string was not in the correct format
Input string was not in the correct format Anybody know why this error is happening? Input string was not in the correct format System.Int32.Parse (System.String s) This is my script.
i'm use ARDUINO in this Script
my script :
using UnityEngine; using System.Collections; using System.IO.Ports;
public class DiceSwipeControl : MonoBehaviour { SerialPort myport = new SerialPort ("\\\.\\COM13",9600);
// Static Instance of the Dice
public static DiceSwipeControl Instance;
#region Public Variables
// Orignal Dice
public GameObject orignalDice;
//dice resultant number..
public int diceCount;
//dice play view camera...
public Camera dicePlayCam;
//Can Throw Dice
public bool isDiceThrowable = true;
public GUIText gui;
public Transform diceCarrom;
#endregion
#region Private Varibles
private GameObject diceClone;
private Vector3 initPos;
private float initXpose;
private float timeRate;
// To Save Camera Postion
private Vector3 currentCampPos;
Vector3 objectPos;
internal float diceThrowInit;
#endregion
void Awake ()
{
Instance = this;
}
void Start ()
{
myport.Open ();
generateDice ();
}
void Update ()
{
string datadariarduino = myport.ReadLine ();
int dfai = int.Parse (datadariarduino);
print ("arduino siap!");
print (dfai);
if (isDiceThrowable)
{
if (dfai == 1)
{
initPos = Input.mousePosition;
initXpose = dicePlayCam.ScreenToViewportPoint (Input.mousePosition).x;
}
Vector3 currentPos = Input.mousePosition;
currentPos.z = 25f;
Vector3 newPos = dicePlayCam.ScreenToWorldPoint (new Vector3(currentPos.x,currentPos.y,Mathf.Clamp(currentPos.y/10,5,70)));
newPos.y = Mathf.Clamp(newPos.y, -114.5f, 100);
newPos = dicePlayCam.ScreenToWorldPoint (currentPos);
if (Input.GetMouseButtonUp (0))
{
initPos = dicePlayCam.ScreenToWorldPoint (initPos);
enableTheDice ();
addForce (newPos);
isDiceThrowable = false;
StartCoroutine (getDiceCount ());
}
}
}
void addForce (Vector3 lastPos)
{
diceClone.GetComponent<Rigidbody>().AddTorque(Vector3.Cross(lastPos, initPos) * 1000, ForceMode.Impulse);
lastPos.y += 12;
diceClone.GetComponent<Rigidbody>().AddForce (((lastPos - initPos).normalized) * (Vector3.Distance (lastPos, initPos)) * 30 * diceClone.GetComponent<Rigidbody>().mass);
}
void enableTheDice ()
{
diceClone.transform.rotation = Quaternion.Euler (Random.Range (0, 180), Random.Range (0, 180), Random.Range (0, 180));
diceThrowInit = 0;
}
void generateDice ()
{
diceClone = Instantiate (orignalDice, dicePlayCam.transform.position, Quaternion.Euler (Random.Range (0, 180), Random.Range (0, 180), Random.Range (0, 180))) as GameObject;
}
#region Coroutines
IEnumerator getDiceCount ()
{
//Time.timeScale = 1.3f;
currentCampPos = dicePlayCam.transform.position;
//wait fore dice to stop...
yield return new WaitForSeconds (1.0f);
while (diceClone.GetComponent<Rigidbody>().velocity.magnitude > 0.05f) {
yield return 0;
}
Time.timeScale = 0.2f;
timeRate = 0.001f;
float startTime = Time.time;
Vector3 risePos = dicePlayCam.transform.position;
Vector3 setPos = new Vector3 (diceCarrom.position.x, diceClone.transform.position.y + 25f, diceCarrom.position.z);
float speed = 0.18f;
float fracComplete = 0;
while (Vector3.Distance(dicePlayCam.transform.position,setPos)>0.5f)
{
Vector3 center = (risePos + setPos) * 0.5f;
center -= new Vector3 (0, 2, -1);
Vector3 riseRelCenter = risePos - center;
Vector3 setRelCenter = setPos - center;
if (fracComplete > 0.85f && fracComplete < 1f) {
speed += Time.deltaTime * 0.3f;
Time.timeScale -= Time.deltaTime * 4f;
}
dicePlayCam.transform.position = Vector3.Slerp (riseRelCenter, setRelCenter, fracComplete);
dicePlayCam.transform.position += center;
dicePlayCam.transform.LookAt (diceCarrom);
fracComplete = (Time.time - startTime) / speed;
yield return 0;
}
Time.timeScale = 1.0f;
Dice.Instance.GetDiceCount();
gui.text = "Dice Count : " + Dice.Instance.GetDiceCount();
yield return new WaitForSeconds (5f);
Dice.Instance.diceCount = 0;
Dice.Instance.GetDiceCount ();
diceCount = Dice.Instance.GetDiceCount ();
gui.text = "Dice Count : " + diceCount;
diceClone.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
Application.LoadLevel(0);
}
#endregion
}
Comment
Answer by LoBlanc · Dec 13, 2016 at 10:26 AM
A way to find out what's failing could be surrounding the parse instructions (which throw exceptions) with a try / catch block. Then you can capture the exception and analyse it to see what's wrong.