The question is answered, right answer was accepted
Try part of C# not being Read
I'm not very good at code so perhaps someone can point me in the right direction as to why this part of the code is not being reached. If I comment all of the update out above it then it does read. So I'm sure I'm buggering up something not being too familiar with it.
try
{
MoveObject(sp.ReadByte());
print(sp.ReadByte());
}
catch (System.Exception) {
}
This is the code with the rest of the Update.
// Update is called once per frame
void Update () {
amountToMove = speed * Time.deltaTime; // New Test For Arduino Move Cube
//print("BytesToRead" +sp.BytesToRead);
message2 = sp.ReadLine();
string message = sp.ReadLine(); //get the message...
if(message == "") return; //if its empty stop right here
// parse the input to a float and normalize it (range 0..1) (we could do this already in the Arduino)
float input = 1 - float.Parse (message) / 100f;
// set the slider to the value
float oldValue = slider.value; // -------- this is new
slider.value = input;
// after the slider is updated, we can check for the other things for example play sounds:
if (source.isPlaying) return; // if we are playing a sound stop here
// else check if we need to play a sound and do it
if (slider.value > 0.9f && oldValue <= 0.9f) // ---------this has changed
source.PlayOneShot (BrightnessAudioClips [Random.Range (0, BrightnessAudioClips.Length)]);
else if (slider.value < 0.15f && oldValue >= 0.15f) //----------this has changed
source.PlayOneShot (DarknessAudioClips [Random.Range (0, DarknessAudioClips.Length)]);
try
{
MoveObject(sp.ReadByte());
print(sp.ReadByte());
}
catch (System.Exception) {
}
}
Ok I found that if I moved the Try and the catch to the top of the Update Then it seems to work although for my particular purpose I'm not sure if this is the best way to do it.
The button press data from the Arduino is just going to be used to activate an animation on another script and game object, that's if I can get some help with that whole other cluster F.... ;)
Answer by KnightRiderGuy · Dec 25, 2015 at 02:03 AM
Well, I'm surprises no one chimed in on this seeing how easy a fix it turned out to be. Seems I had to place the Try and the Catch at the beginning of the Update.