Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by DAlturG · Apr 20, 2016 at 05:11 PM · scripting problemspawnlagspawningadvice

Script causing lag

I have a spawning script that spawns objects in three different locations every x seconds. The objects place themselves on the right path after they are spawned as they have a separate script , but that isn't the problem. This script is. Any suggestions on how it could be better and not cause any lag? Thanks Script:

 var power : Rigidbody[];
 var carsL : Rigidbody[];/////spawns on left side
 var carsM : Rigidbody[];/////spawns in middle
 var carsR : Rigidbody[];///////spawns on right side

   
 var spawnWaitTime1 = 0.5;
 var spawnWaitTime2 = 0.5;
 var spawnWaitTime3 = 0.5;
 var prefabs : int;
 var indexL : int;
 var indexM : int;
 var indexR : int;
 var index : int;
 public var where : Transform;
 
 function Start () 
 {
    Invoke("Spawn1", 1);
     Invoke ("Spawn2", 1);
     Invoke ("Spawn3", 1);
  spawnWaitTime1 = Random.Range(0.3,1);
   spawnWaitTime2 = Random.Range(0.3,0.8);
   spawnWaitTime3 = Random.Range(0.3,0.8);
 
 }
 
   
 function Spawn1 () 
 {
 prefabs = 1;
 Invoke("Spawn1", 2);
 if(prefabs == 1 ){
          indexL= Random.Range (0, carsL.Length);
         var object1L : Rigidbody = carsL[indexL];
      object1L = Instantiate(object1L, where.position, where.rotation);
      object1L.AddForce(Vector3(100,0,0));
      if(indexL == 1){
      spawnWaitTime1 = Random.Range(0.8,0.9);
      }
 }
    if(prefabs == 2 && Random.value > 0.6){///// This hasn't been set yet
      index = Random.Range (0, power.Length);
      var powerup1 : Rigidbody = power[index];
  powerup1 = Instantiate(powerup1, where.position, where.rotation);
     powerup1.AddForce(Vector3(100,0,0));
     }
     if(prefabs == 2 && Random.value < 0.6){
     prefabs = 1;
     }
 
     
  
 function Spawn2 () 
 {
 Invoke("Spawn2", 2);
 prefabs = 1;
 
 if(prefabs == 1 ){
 
      indexM = Random.Range (0, carsM.Length);
         var object1M : Rigidbody = carsM[indexM];
      object1M = Instantiate(object1M, where.position, where.rotation);
      object1M.AddForce(Vector3(100,0,0));
       if(indexM == 1){
      spawnWaitTime2 = Random.Range(0.8,0.9);
      }
 }
    if(prefabs == 2 && Random.value > 0.6){
      index = Random.Range (0, power.Length);
      var powerup1 : Rigidbody = power[index];
  powerup1 = Instantiate(powerup1, where.position, where.rotation);////for powerups
     powerup1.AddForce(Vector3(100,0,0));
     }
     if(prefabs == 2 && Random.value < 0.6){
     prefabs = 1;
     }
 
     
  
 }
 
 function Spawn3 () 
 {
 Invoke("Spawn3", 2);
 prefabs = 1;
 
 if(prefabs == 1 ){
    
      indexR = Random.Range (0, carsR.Length);
         var object1R : Rigidbody = carsR[indexR];
      object1R = Instantiate(object1R, where.position, where.rotation);
      object1R.AddForce(Vector3(100,0,0));
       if(indexR == 1){
      spawnWaitTime3 = Random.Range(0.8,0.9);
      }
 
 }
    if(prefabs == 2 && Random.value > 0.6){/////powerups
      index = Random.Range (0, power.Length);
      var powerup1 : Rigidbody = power[index];
  powerup1 = Instantiate(powerup1, where.position, where.rotation);////for powerups
     powerup1.AddForce(Vector3(100,0,0));
     }
     if(prefabs == 2 && Random.value < 0.6){
     prefabs = 1;
     }
 
     
  
 
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tanoshimi · Apr 20, 2016 at 06:17 PM

Yes. Object pooling.

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 DAlturG · Apr 22, 2016 at 06:12 PM 0
Share

Thanks, this looks like it would solve my problem but I have trouble making it spawn random objects. The script just pools one single random object from array ins$$anonymous$$d of pooling all random objects. Heres my script :

using UnityEngine; using System.Collections; using System.Collections.Generic; public class Fire : $$anonymous$$onoBehaviour {

 public float fireTime = 0.5f;
 public GameObject[] bullet;
 public static float far = 0;
 public int pooledAmount = 20;
 public List<GameObject> cars;
 public Transform where;
 int index;
 void Start()
 {
     
     cars = new List<GameObject> ();
     for (int i = 0; i < pooledAmount; i++) {
         GameObject obj = (GameObject)Instantiate (bullet[index]);
         obj.SetActive (true);
         cars.Add (obj);
     }
     InvokeRepeating ("Fired", fireTime, fireTime);
 }

 void Update(){
     index = Random.Range (0, bullet.Length);
 }
 void Fired()
 {
     
     for(int i = 0; i < cars.Count; i++)
     {
         if(!cars[i].activeInHierarchy)
         {
             far = 0;
             cars[i].transform.position =new Vector3(0,0,0);
             cars[i].transform.rotation = where.rotation;
             cars[i].SetActive(true);
             break;
         }
     }
 }

}

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I spawn without glitch 1 Answer

How do I get an object to Instantiate infront of a specific object/player? 1 Answer

Spawn an object with additional parameter 1 Answer

Serious performance issues 0 Answers

i'm trying to spawn enemies and it goes well but when i'm trying to scale them it doesn't sync i used [ClientRpc] and it didn't work 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