restricting object clones numbers.

Hey i am using this script and want to be able to restrict how many clones i can spawn. i am spawning ramps and only want the player to be able to spawn say 5 per level. and when they run out they simple cant use anymore. THanks

var projectile : Rigidbody;

var speed = 0;

function Update() {
if( Input.GetButtonDown( “Fire1” ) ){

var instantiatedProjectile : Rigidbody = Instantiate( projectile, transform.position, transform.rotation );

instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) );

Physics.IgnoreCollision( instantiatedProjectile.collider , transform.root.collider );

}}

3 Answers

3

Pretty Simple. Just create a var max_projectiles and another var projectile_count and then set it, to say, 5. then check if it is equal to 5 after pressing the button, if its not, add 1 to it. For example:

   var projectile : Rigidbody;
    var max_projectiles = 5;
    var projectile_count = 0;

var speed = 0;

function Update() { if( Input.GetButtonDown( "Fire1" ) ){
if (projectile_count <= max_projectiles)
{
var instantiatedProjectile : Rigidbody = Instantiate( projectile, transform.position, transform.rotation );

instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) );

Physics.IgnoreCollision( instantiatedProjectile.collider , transform.root.collider );
projectile_count++
}

}}

not tested so dont know if it will work. tweak it, its really simple.

THanks for your help, i needed to put in a } at the end but it doesnt seem to working. it just keeps spawning after 5 like before?

Dont worry, figured it out. Thanks heaps :slight_smile: