- Home /
Can't get Object size to Reset to Original
I have a game object that grows for a certain amount of time, then once it reaches a set size it duplicates itself. However... I'd like the duplicated object to be reset back to the original size (so it can go through the grow process again before duplicating itself)
#pragma strict
public var circleRadius : float;
public var waitTime : float = 2f;
private var hasDuplicated : boolean = false;
private var fullyGrown : boolean = false;
function Update()
{
if(fullyGrown == false)
{
if(transform.localScale.x >= 3.0 && transform.localScale.y >= 3.0 && transform.localScale.z >= 3.0 )
{
fullyGrown = true;
StartCoroutine(WaitAndDuplicate());
}
else
if(transform.localScale.x < 3.0 && transform.localScale.y < 3.0 && transform.localScale.z < 3.0 )
{
transform.localScale += Vector3(0.001,0.001,0.001); // Increase object size by .01 per frame
}
}
}
function WaitAndDuplicate()
{
if(hasDuplicated == false)
{
yield WaitForSeconds(waitTime); var randomOffset : Vector3 = Random.insideUnitCircle * circleRadius; randomOffset = Vector3(randomOffset.x, randomOffset.z, randomOffset.y); Instantiate(gameObject, transform.position + randomOffset, transform.rotation);
hasDuplicated = true;
}
}
At the moment, the game object grows to full size, waits 2 seconds then duplicates itself. The trouble is the duplicate is already fully grown (which I don't want)
How do I get each new duplicate to start off at the original size?
When you instantiate, it returns the object that was created. There you can just set the scale to what you want it to start at. Or change the prefab to have the correct scale.
Obviously I'm missing something here. Changed the script to -
#pragma strict
public var circleRadius : float;
public var waitTime : float = 2f;
private var hasDuplicated : boolean = false;
private var fullyGrown : boolean = false;
public var newCrystal : GameObject;
function Update()
{
if(fullyGrown == false)
{
if(transform.localScale.x >= 3.0 && transform.localScale.y >= 3.0 && transform.localScale.z >= 3.0 )
{
fullyGrown = true;
StartCoroutine(WaitAndDuplicate());
}
else
if(transform.localScale.x < 3.0 && transform.localScale.y < 3.0 && transform.localScale.z < 3.0 )
{
transform.localScale += Vector3(0.001,0.001,0.001); // Increase object size by .01 per frame
}
}
}
function WaitAndDuplicate()
{
if(hasDuplicated == false)
{
yield WaitForSeconds(waitTime); var randomOffset : Vector3 = Random.insideUnitCircle * circleRadius; randomOffset = Vector3(randomOffset.x, randomOffset.z, randomOffset.y); Instantiate(newCrystal, transform.position + randomOffset, transform.rotation);
hasDuplicated = true;
}
}
The newCrystal : GameObject has the prefab on it in the Inspector. And as I'm now instantiating the prefab (XYZ scale of 1) shouldn't the instantiated object have an XYZ scale of 1 also ???
Answer by InvincibleCat · Feb 12, 2015 at 07:37 PM
Just add private var originalScale : Vector3 = Vector3.zero;
Then on the Start method:
function Start()
{
originalScale = transform.lossyScale;
}
And when replace your instanciate method with:
var newCrystalGO : GameObject = Instantiate(newCrystal, transform.position + randomOffset, transform.rotation);
newCrystalGO.localScale = originalScale;
And the full script will be
#pragma strict
public var circleRadius : float;
public var waitTime : float = 2f;
private var hasDuplicated : boolean = false;
private var fullyGrown : boolean = false;
public var newCrystal : GameObject;
private var originalScale : Vector3 = Vector3.zero;
function Start()
{
originalScale = transform.lossyScale;
}
function Update()
{
if(fullyGrown == false)
{
if(transform.localScale.x >= 3.0 && transform.localScale.y >= 3.0 && transform.localScale.z >= 3.0 )
{
fullyGrown = true;
StartCoroutine(WaitAndDuplicate());
}
else
if(transform.localScale.x < 3.0 && transform.localScale.y < 3.0 && transform.localScale.z < 3.0 )
{
transform.localScale += Vector3(0.001,0.001,0.001); // Increase object size by .01 per frame
}
}
}
function WaitAndDuplicate()
{
if(hasDuplicated == false)
{
yield WaitForSeconds(waitTime);
var randomOffset : Vector3 = Random.insideUnitCircle * circleRadius;
randomOffset = Vector3(randomOffset.x, randomOffset.z, randomOffset.y);
var newCrystalGO : GameObject = Instantiate(newCrystal, transform.position + randomOffset, transform.rotation);
newCrystalGO.transform.localScale = originalScale;
hasDuplicated = true;
}
}
Cheers