- Home /
Access Transform of an Array of GameObjects
Okay I know this is such a long winded way of writing it but I gave it a go, could anyone possible explain how I can simplify this whole piece of script.
To explain what I'm doing: I have two game objects that are instantiated at different points in my game, both scale from 0.01 to 1.00, I want the script to recognize when both objects have been instantiated and when they're both full scale.
var runonce = false; var runagain = false; var objectAdd = false; var fullScaleObject = 0;
function Update(){
var objectCount : GameObject[] = gameObject.FindGameObjectsWithTag("myObject");
if(objectAdd){
addAnObject();
}
if(fullScaleObject == 2){
Debug.Log("COMPLETE");
}
if(objectCount[0].transform.localScale.y >= 1.0){
if(!runonce){
addAnObject = true;
runonce = true;
}
}
if(objectCount[1].transform.localScale.y >= 1.0){
if(!runagain){
addAnObject = true;
runagain = true;
}
}
}
function addAnObject(){
fullScaleObject += 1;
objectAdd = false;
}
Thanks - C
Answer by Kourosh · Apr 19, 2011 at 01:54 PM
Why don't you attach a single script to each object that you want scaled:
scaleScript.js
var increment:float; var speed:float = 0.001; var originalScale:Vector3;
function Start(){ originalScale = transform.localScale; transform.localScale = Vector3.zero; } function LateUpdate(){ transform.localScale = Vector3.Lerp(transform.localScale,originalScale,increment ); if(increment <1) increment += speed; }
so they we'll be scaled to their original scale from zero, as soon as they are instantiated.
This seems the simplest way to go. scaleScript.js could then also message your manager script when instantiated, and when at scale 1. Your manager script could then keep track of how many messages it's received and respond accordingly when all objects have reported in.
Okay I wasn't really after a scaling script, probably bad explanation on my behalf. I just wanted an simple way of writing what I have in the script above... but your suggestion of sending a message to the manager when the object have scaled fully works a treat, so you've indirectly answered my question! Thanks.