- Home /
Writing data from multiple instances of a script to text
Hello, i have multiple instances of a script called TransformExtractor running on separate game objects, This script return transform data and mass for a object it is attached to. I need to write the Transform data and Mass to a text file off all instances of the script running and assign each one a key ( starting at 1 ?). What is the simplest way of going about this?
Thanks in advance, Bob
using UnityEngine; using System.Collections; using System.IO; public class TransformExtractor : MonoBehaviour { public GameObject player; public float x; public float y; public float z; public float Qx; public float Qy; public float Qz; public float Qw; public float scaleX; public float scaleY; public float scaleZ; public float Mass; // Use this for initialization void Update () { player = this.gameObject; x = player.transform.position.x; y = player.transform.position.y; z = player.transform.position.z; Qx = player.transform.rotation.x; Qy = player.transform.rotation.y; Qz = player.transform.rotation.z; Qw = player.transform.rotation.w; scaleX = player.transform.localScale.x; scaleY = player.transform.localScale.y; scaleZ = player.transform.localScale.z; } }
This is what i have so far, but it return nothing, when the button i made in the editor is pressed. From the Debug.Log, the function is being called but the Length of transformExtractor is 0.
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
public class KS_ObjectInfoGetter : MonoBehaviour
{
public TransformExtractor[] transformExtractors;
public void KSprint ()
{
Debug.Log ("KSprint Called");
Debug.Log (transformExtractors.Length);
transformExtractors = GetComponents<TransformExtractor> ();
int i;
for(i = 1; i < transformExtractors.Length ; i++)
{
Debug.Log(i.ToString () + "," +
transformExtractors [i].x.ToString () + "," +
transformExtractors [i].y.ToString () + "," +
transformExtractors [i].z.ToString () + "," +
transformExtractors [i].Qx.ToString () + "," +
transformExtractors [i].Qy.ToString () + "," +
transformExtractors [i].Qz.ToString () + "," +
transformExtractors [i].scaleX.ToString () + "," +
transformExtractors [i].scaleY.ToString () + "," +
transformExtractors [i].scaleZ.ToString () + "," +
transformExtractors [i].Mass.ToString () + ","
);
}
}
}
Answer by Jeff-Kesselman · May 09, 2014 at 12:04 AM
Assign each instance a unique value by using a static counter.
public class MySerialnumberedClass: MonoBehaviour{
static int LastID=0;
int myID;
void Start (){
myID = LastID++;
}
}
once i add a 'identifier' number to each instance how do i find all the instances and write them to a list?
thanks for the reply
Your answer
