Home > Software engineering >  how do i get c# code to print position and rotation values from inspector to console?
how do i get c# code to print position and rotation values from inspector to console?

Time:08-06

i am a beginner at unity and C# and struggling to get the position nad rotation valuse form the inspector to the console i am a beginner at unity and C# and struggling to get the position nad rotation valuse form the inspector to the console

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class positions : MonoBehaviour
{
    float rx;
    float ry;
    float rz;
    float tx;
    float ty;
    float tz;
    // Start is called before the first frame update
    void Start()
    {
           Debug.Log(rx);
           Debug.Log(ry);
           Debug.Log(rz);
           Debug.Log(tx);
           Debug.Log(ty);
           Debug.Log(tz);
        
        print(rx);



    }

    // Update is called once per frame
    void Update()
    {
        rx = GetComponent<Transform>().rotation.x; 
        ry = transform.localEulerAngles.y;
        rz = transform.localEulerAngles.z;

        tx = transform.position.x;
        ty = transform.position.y;
        tz = transform.position.z;

    }
}

CodePudding user response:

I assume you mean update the console every frame?

If that is the case, then you would need to do this in the Update() method.

The Start() method is only called once, just before the first frame is updated.

The Update() method is called once per frame.

You can see this in the auto comment above each method in your code.

void Update()
{
    rx = GetComponent<Transform>().rotation.x; 
    ry = transform.localEulerAngles.y;
    rz = transform.localEulerAngles.z;

    tx = transform.position.x;
    ty = transform.position.y;
    tz = transform.position.z;

    Debug.Log(rx);
    Debug.Log(ry);
    Debug.Log(rz);
    Debug.Log(tx);
    Debug.Log(ty);
    Debug.Log(tz);
}

The above code would print the values to the console once for every frame.

An example can be seen in the following screenshot:

enter image description here

You can also see in the bottom left hand corner that the most recent print statement is displayed.

Hope this helps.

CodePudding user response:

You can get the inspector's rotations using TransformUtils.GetInspectorRotation and using transform.position to get the position as Vector3

Vector3 rv;
Vector3 p;

void Update()
{
rv = UnityEditor.TransformUtils.GetInspectorRotation(transform);
Debug.Log("Rotations: "  rv.x  " " rv.y " " rv.z);
p  = transform.position;
Debug.Log("Positions: "  p.x  " " p.y " " p.z);
}
  • Related