Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-3
Question by darkal · Feb 23, 2014 at 06:17 PM · javascriptinstantiatespawncount

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;
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dre38w · Jul 24, 2014 at 10:31 AM 0
Share

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.

avatar image
3

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image darkal · Feb 23, 2014 at 06:35 PM 0
Share

i just need it to Instantiate the exact amount i set in the var amount which atm is one

avatar image YoungDeveloper · Feb 23, 2014 at 06:40 PM 0
Share

That isn't very smart comment.. then change it to other number?? Or make it public and change it from inspector.

avatar image darkal · Jul 26, 2014 at 08:38 AM 0
Share

??? are people really still looking at this i made it forever ago lmao

avatar image
0

Answer by SuperRyn · Jan 10, 2019 at 12:24 PM

i need help too

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges