- Home /
instantiate a set amount
i want to spawn a certain amount each time
var health : int;
var details : boolean = false;
//this is the amount i want it to spawn but i wanna be able 2 change it
var amount = 1;
var Item;
var item1 : GameObject;
var item2 : GameObject;
var item : Transform;
var damage : float = 5;
var itemname = "Stone";
var level : int;
function Start ()
{
level = Random.Range(1, 10);
health = hitcount * level * 10;
}
function Update()
{
if(hit)
{
health -= (Random.Range(0, 100));
hit = false;
}
if(health <= 0)
{
if (Random.value <= 0.5)
{
Item = item1;
}
else
{
Item = item2;
}
//need to set the amount here but unsure of how
Instantiate(Item, item.position, Quaternion.identity);
Destroy(this.gameObject);
}
}
function OnGUI()
{
if(details)
{
GUI.Box(Rect(0, 0, 100, 75), itemname);
GUI.Box(Rect(0, 25, 100, 25), level.ToString());
GUI.Box(Rect(0, 50, 100, 25), health.ToString());
}
}
function OnTriggerEnter(other : Collider)
{
if(other.tag == "item1")
{
hit = true;
}
if(other.tag == "item2")
{
details = true;
}
}
function OnTriggerExit(other : Collider)
{
if(other.tag == "item2")
{
details = false;
}
}
Answer by darkal · Feb 23, 2014 at 07:14 PM
i got it and it works just the way i want it to thanks anyway
var hit : boolean = false;
var hitcount = 10;
var health : int;
var details : boolean = false;
//this is the amount i want it to spawn but i wanna be able 2 change it
var amount = 1;
var Item;
var item1 : GameObject;
var item2 : GameObject;
var item : Transform;
var damage : float = 5;
var itemname = "Stone";
var level : int;
function Start ()
{
level = Random.Range(1, 10);
health = hitcount * level * 10;
}
function Update()
{
if(hit)
{
health -= (Random.Range(0, 100));
hit = false;
}
if(health <= 0)
{
if (Random.value <= 0.5)
{
Item = item1;
}
else
{
Item = item2;
}
//need to set the amount here but unsure of how
if(amount == 1)
{
Instantiate(Item, item.position, Quaternion.identity);
}
if(amount == 2)
{
Instantiate(Item, item.position, Quaternion.identity);
Instantiate(Item, item.position, Quaternion.identity);
}
if(amount == 3)
{
Instantiate(Item, item.position, Quaternion.identity);
Instantiate(Item, item.position, Quaternion.identity);
Instantiate(Item, item.position, Quaternion.identity);
}
if(amount == 4)
{
Instantiate(Item, item.position, Quaternion.identity);
Instantiate(Item, item.position, Quaternion.identity);
Instantiate(Item, item.position, Quaternion.identity);
Instantiate(Item, item.position, Quaternion.identity);
}
if(amount == 5)
{
Instantiate(Item, item.position, Quaternion.identity);
Instantiate(Item, item.position, Quaternion.identity);
Instantiate(Item, item.position, Quaternion.identity);
Instantiate(Item, item.position, Quaternion.identity);
Instantiate(Item, item.position, Quaternion.identity);
}
Destroy(this.gameObject);
}
}
function OnGUI()
{
if(details)
{
GUI.Box(Rect(0, 0, 100, 75), itemname);
GUI.Box(Rect(0, 25, 100, 25), level.ToString());
GUI.Box(Rect(0, 50, 100, 25), health.ToString());
}
}
function OnTriggerEnter(other : Collider)
{
if(other.tag == "item1")
{
hit = true;
}
if(other.tag == "item2")
{
details = true;
}
}
function OnTriggerExit(other : Collider)
{
if(other.tag == "item2")
{
details = false;
}
}
This is $$anonymous$$ONTHS old but use a for loop. $$anonymous$$uch cleaner than a bunch of ifs. At least when you get to the point of cleaning up your code before you release your game.
Answer by YoungDeveloper · Feb 23, 2014 at 06:28 PM
You didn't really tell us much of how you want to instantiate them or how it should work. And what should actually change the "amount".
You can create a method which handles the amount set, or call a method and pass amount it should instantiate.
//Passing the value to the method
//On event you want to instantiate
var theAmount : int = Random.Range(0,100); //amount i want to spawn
Spawn(theAmount);
function Spawn(int amount){
for(var i:int =0; i<amount; i++){
Instantiate(Item, item.position, Quaternion.identity); ///will spawn amount of times given
}
}
Or just call the method, which handles everything.
Spawn();
function Spawn(){
var amount:int = Random.Range(0,100);
for(var i:int =0; i<amount; i++){
Instantiate(Item, item.position, Quaternion.identity); ///will spawn amount of times given
}
}
There is universal way, everything depends of your code structure.
i just need it to Instantiate the exact amount i set in the var amount which atm is one
That isn't very smart comment.. then change it to other number?? Or make it public and change it from inspector.
??? are people really still looking at this i made it forever ago lmao
Your answer
Follow this Question
Related Questions
Enemy spawn script not working properly ! 1 Answer
Assign the Instantiated Object to an Empty Object. 1 Answer
Issue With Spawning Enemies (javascript) 2 Answers
Machine gun script shoots all bullets at once 2 Answers
Script needs fixing ?! 1 Answer