code with serialport

hy guys i need help with this script, i have an instrument that can emet odor, in the game the player, with a vr, can approach the object that make odor and the idea behind is that i want an increment of the odor if the player mooved towards the object, but there is a problem with my instrument, the problem is that it cant make a costant increase but every 10 step the odor valve opened by 10, steps are calcolated thanks to the distance form the camera (vr) and the object. for my script i make 3 state for the valve of my instrument…the problem that i have is that che value of the valve and the steps dont increment. the script of the olfactometer is without error and is connected well with the instrument…sorry fod bad english, hope you can understand, oh and the values of maxdistance i see in the game the distance.

using UnityEngine;

public class Smellint : MonoBehaviour
{
    public Transform playerCamera;
    public Olfactometer olfactometer;
    private float maxDistance = 1.9f;
    private float maxValveOpening = 100f;
    private float minValveOpening = 0f;
    private float stepValveOpening = 10f;
    private float valveOpening = 0f;
    private int state = 0;

    private const int STEP_COUNT = 100;
    private float stepDistance;
    private float lastStepDistance;

    private int lastStep = 0;

    private void Start()
    {
        stepDistance = maxDistance / STEP_COUNT;
        lastStepDistance = 0f;
    }

    private int lastState = -1;
    private float lastDistance = -1f;
    private bool stateChanged = false;
    private bool isValveOpened = false;

    private void Update()
    {
        float distance = Vector3.Distance(transform.position, playerCamera.position);

        if (lastState != state || Mathf.Abs(distance - lastDistance) >= 0.1f)
        {
            stateChanged = true;
        }
        if (stateChanged)
        {
            if (lastState != state)
            {
                //Debug.Log("Current state: " + state);
                lastState = state;
            }
            if (Mathf.Abs(distance - lastDistance) >= 0.1f)
            {
                Debug.Log("Distance: " + distance);
                Debug.Log("step: " + lastStep);
                lastDistance = distance;
            }
            stateChanged = false;
        }

        if (distance > maxDistance)
        {
            state = 0;
            // Debug.Log("state: " + state);
            // Debug.Log("valveOpening: " + valveOpening);
        }
        else if (distance <= maxDistance)
        {
            state = 1;
           // Debug.Log("state: " + state);
           // Debug.Log("valveOpening: " + valveOpening);
        }
        if (state == 0)
        {
            valveOpening = 0f;
        }
        else if (state == 1 && !isValveOpened)
        {
            olfactometer.write("setValve 1\r");
            olfactometer.write("readflow\r");
            Debug.Log("Valve opened");
            isValveOpened = true;
            state = 2;
        }
        if (state == 2)
        {
            int step = Mathf.RoundToInt(distance / stepDistance);

            Debug.Log("step: " + step);

            if (step >= STEP_COUNT) step = STEP_COUNT - 1;

            if (step > lastStep + 0.171 && valveOpening < maxValveOpening)
            {
                valveOpening = Mathf.Clamp(valveOpening + stepValveOpening, minValveOpening, maxValveOpening);
                olfactometer.write("steps " + valveOpening + "\r");
                Debug.Log("valve opening set to: " + valveOpening);
                lastStep = step;
            }
            else if (step > lastStep - 0.171 && valveOpening > minValveOpening)
            {
                valveOpening = Mathf.Clamp(valveOpening - stepValveOpening, minValveOpening, maxValveOpening);
                olfactometer.write("steps " + valveOpening + "\r");
                Debug.Log("valve opening set to: " + valveOpening);
                lastStep = step;
            }
            lastStepDistance = distance;
        }
    }
}

That is an ENORMOUS bunch of code and numbers and computations to reason about all at once… you’re probably going to have to section it out and find where your computations are going wrong. Here’s how:

Time to start debugging! Here is how you can begin your exciting new debugging adventures:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the names of the GameObjects or Components involved?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

“When in doubt, print it out!™” - Kurt Dekker (and many others)

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.