Transform Unity Object with Arduino

hey guys hope you can help me!
i try to use the serial numbers from my arduino board to transform the object in unity. on arduino site my code is really simple and looks like this:

  void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int a = analogRead(A0);
  //Sleep for 50ms, which provides the recommended sample rate (20Hz)
  delay(500);
  Serial.println(a);
}

in the serial monitor from arduino i get these values (picture)

now i want to use these values to constantly transform my geometry (a cube) in unity. for example if the value from arduino is 50 so the cube should be scaled by 50

do you know how this can work???

thanks in advance

You need to put that code in a custom MonoBehavior class. Then, you will be able to make that loop function, your monobehavior’s “Update” function instead (and setup() stuff inside the monobehavior’s Start() function). I would suggest you create a cube in your scene using the unity menu, then ADD this new monobehavior component you just created, to the gameobject.

Monobehavior classes, when attached to a game object as a component, can access the object’s Transform. This is how you will change it’s size, inside Update():

transform.localScale= Vector3.one * analogRead(A0);

hey thanks for your answer…so it doesnt sound that easy any more :smiley:

do you think (on unity side) it could work with something like this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;

public class Scale : MonoBehaviour
{

    private float analogRead;

    SerialPort sp = new SerialPort("COM3", 9600);


    // Use this for initialization
    void Start()
    {

        sp.Open();
        sp.ReadTimeout = 1;


    }

    // Update is called once per frame
    void Update()
    {

        analogRead = analogRead;

        if (sp.IsOpen)
        {
            ScaleObject(sp.ReadByte());
        }
    }

    void ScaleObject(int analogRead)
    {
        transform.localScale = Vector3.one * analogRead;

    }
}

kind regards

yeah tried to change it to public but you can not see a change, i mean it gets closer but still not the thing that i want

now i have the problem that the geometry gets some kind of “disco flash light”-effect because the scale always goes back to the original value…for example 34,1,23,1,33,1,12,1 and so on

never thought that this could be so tricky