Spawn Object at Original Size not Current Size
Hello All!
I am very new to code and am currently trying to make my first mobile game where a bunch of growing orbs (spheres) appears on the screen and the player must destroy them before they touch each other.
I am struggling to spawn the orbs at their original size and not there current size.
public class orbScript : MonoBehaviour {
public float scaleRate = 0.01f;
public float spawnTime = 5f;
// Use this for initialization
void Start ()
{
StartCoroutine(SpawnTimer());
}
// Update is called once per frame
void Update ()
{
//grow the orb
transform.localScale += new Vector3(scaleRate, scaleRate, 0);
}
//timer for the spawn function, this waits x seconds before running spawn function
IEnumerator SpawnTimer()
{
while (true)
{
yield return new WaitForSeconds(spawnTime);
Spawn();
}
}
//spawns gameObject at a random x and y
void Spawn()
{
float randomX = Random.Range(-2.9f, 2.9f);
float randomY = Random.Range(-4.9f, 4.9f);
Instantiate(gameObject, new Vector3(randomX, randomY, 0), Quaternion.identity);
}
void OnMouseDown()
{
Destroy(gameObject);
}
}
Is it possible to make adjust the spawn() function so that it Instantiates the gameObject at its original size and not the updated size but continues to "grow" once spawned?
Thank you very much in advance!
Answer by Cynikal · Oct 08, 2017 at 06:46 PM
Easiest way, is to add a reference.
Add a Vector3 OriginalSize outside of your scopes (near scalerate & spawntime).
Then on Start, set the first line to OriginalSize = gameObject size.
Then, on spawn or whatever, just call for the originalsize.
Thanks very much Cynikal :)
I have created a variable for originalsize which gets the starting size of the game object. But as far as I can tell the Instantiate function doesnt allow you to pass it a scale, only a position and rotation.
Is there a way to instantiate the gameObject and give it my originalSize variable to work with?
Hey Cynikal
Disregard the above comment, i figured out what you meant :)
Thanks again for you help!