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 /
  • Help Room /
avatar image
0
Question by Ochreous · Nov 11, 2015 at 06:34 PM · c#loopmultipleinstancesfor

C# Use Multiple instances of Script or For Loop Once

I have two scripts. One accounts for one shooter and the other accounts for multiple shooters. I feel that having both of these is redundant. So what would be best resource wise, using multiple instances of the first script or use the second script once for the gameobjects with only one shooter?

 using UnityEngine;
 using System.Collections;
 public class PlayerShooter: MonoBehaviour 
 {
     //Public Var
     public GameObject shotGO;            //Fire Prefab
     public float baseFireRate;    //Fire Rate between Shots
     public float fireRate = 0.5F;    //Fire Rate between Shots
 
     //Private Var
     private float nextFire = 0.0F;    //First fire & Next fire Time
 
     void Start() 
     {
         baseFireRate = fireRate;
     }
     
     void Update () 
     {
         //Excute When the Current Time is bigger than the nextFire time
         if (Input.GetMouseButton(0)){
             if (Time.time > nextFire) 
             {
                 nextFire = Time.time + fireRate;                                 //Increment nextFire time with the current system time + fireRate
                 Instantiate (shotGO , transform.position ,transform.rotation);     //Instantiate fire shotGO
                 GetComponent<AudioSource>().Play();                                                     //Play Fire sound
             }
         }
     }
 }

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class PlayerShooterMulti: MonoBehaviour 
 {
     //Public Var
     public List<Transform> shotPositions;
     public GameObject shotGO;            //Fire Prefab
     public float baseFireRate;    //Fire Rate between Shots
     public float fireRate = 0.5F;    //Fire Rate between Shots
     //Private Var
     public float nextFire = 0.0F;    //First fire & Next fire Time
 
     void Start() 
     {
         baseFireRate = fireRate;
     }
     
     void Update () 
     {
         //Excute When the Current Time is bigger than the nextFire time
         if (Input.GetMouseButton(0)){
             if (Time.time > nextFire) 
             {
                 nextFire = Time.time + fireRate;                                 //Increment nextFire time with the current system time + fireRate
                 for (int i = 0; i < shotPositions.Count; i++){
                     Instantiate (shotGO , shotPositions[i].position ,shotPositions[i].rotation);     //Instantiate fire shotGO
                 }
                 GetComponent<AudioSource>().Play();                                                     //Play Fire sound
             }
         }
     }
 }
 

 
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
0

Answer by the_genius · Nov 11, 2015 at 06:51 PM

Do what you find easiest for development, as there would be almost no difference. If you wish to improve your game/app resource wise, you should be focusing more on models and textures. The lengths of scripts and how many you use will have very very little effect compared to these.

It is also good practice to keep scripts short (although there are some arguments against this) and it can make development much easier when you can clearly see what each script does straight away. If you combined both scripts with a boolean for 'if it has multiple shooters' which you can then set in the inspector, when looking back on the script in the future it might be quite confusing.

In the end it is your opinion. Personally, I would leave them separate.

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 Ochreous · Nov 11, 2015 at 08:09 PM 0
Share

Well if there's no difference in terms of optimization I guess I'll just go with the second script then. $$anonymous$$ultiple instances of the same script are a bit of a pain to handle.

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

31 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone explain what IEnumerators and for statements are? 1 Answer

How to obtain a variable in update that doesn't behave like a loop? C# 1 Answer

Countdown timer in multiple scene? 1 Answer

Loop animation by script without turning on loop on Animation Inspector 0 Answers

For loop not working 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