Text Array

Hello!! I need help. I try to make array of text of 3D textMesh. So I can call this controller array if object(button) is rotate every 20; the 3d text mesh will appear according to the position text in that array. So I need help here how to make this happen. Thank you.:slight_smile:

Below is the script I apply to the 3D text Mesh.

var textvaluevoltage : Array;

    function Awake() {

     GetComponent(TextMesh).text = textvaluevoltage();
        //add some values to the array,  a list value of text

        textvaluevoltage = Array("0000","0001","0002","0003");

    }

    function Start() {

        //prints out "0000"
        print (textvaluevoltage[0]);

        //prints out "0001"
        print (textvaluevoltage[1]);

        //prints out "0002"
        print (textvaluevoltage[2]);

        //prints out "0003"
        print (textvaluevoltage[3]);

    }

The question is unclear: do you want to show different values in the 3D Text according to the rotation of another object, each value spaced 20 degrees apart? If so, you could use a script like this attached to the button object:

var text3d: TextMesh; // drag the 3DText here
var voltages: String[] = ["0000", "0001", "0002", "0003];
private var rot0: Quaternion;

function Start(){
  // save the initial rotation:
  rot0 = transform.rotation;
}

function Update(){
  // get the angle from starting position:
  var angle = Quaternion.Angle(transform.rotation, rot0);
  // convert to an index (20 degrees per position):
  var index = angle / 20;
  // clamp it to avoid out of range errors:
  index = Mathf.Clamp(index, 0, voltages.length);
  // define the text according to the index:
  text3d.text = voltages[index];
}

Notice that Angle only returns a positive value: it measures the absolute angle between the two rotations, no matter to which side the object has been rotated. This limits the usable rotation to 180 degrees.

If you want to display other intermediate voltages, forget about the array and show the value directly:

var text3d: TextMesh; // drag the 3DText here
private var rot0: Quaternion;

function Start(){
  // save the initial rotation:
  rot0 = transform.rotation;
}

function Update(){
  // get the angle from starting position:
  var angle = Quaternion.Angle(transform.rotation, rot0);
  // convert to a value spaced 20 degrees per unit:
  var volts = angle / 20;
  // define the text according to the index:
  text3d.text = volts.ToString("F3"); // show 3 decimal places
}

C#

using UnityEngine;
using System.Collections;
public class X1 : MonoBehaviour
{
	public Transform monitoringObject;
	public string[] counterValues;
	int currentValueIndex = 0;
	float turnAngleCollected = 0f;
	Quaternion lastRotation;
	void Start()
	{
		lastRotation = monitoringObject.rotation;
		GetComponent().text = counterValues[currentValueIndex];
	}
	void Update()
	{
		turnAngleCollected += Quaternion.Angle(lastRotation, monitoringObject.rotation);
		lastRotation = monitoringObject.rotation;
		while (turnAngleCollected > 20f)
		{
			turnAngleCollected -= 20f;
			currentValueIndex++;
			if (currentValueIndex >= counterValues.Length)
			{
				currentValueIndex = 0;
			}
			GetComponent().text = counterValues[currentValueIndex];
		}
	}
}