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
}
}
}
}
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.
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
Follow this Question
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