How can I separate my Code efficiently ?
I am Working on a Power Up system for a Top Down Shooter in c# For every powerup I write the same for the Pick up of the item and start my Countdown timer where the only major difference between the items and their effects is in the two lines of code that call the methods for what they should do. My code works and the powerups too but instead of repeating that again and again for every item how can I write this more Object Oriented? My item pickup class looks like this(here for a double shield): public class DoubleShield : MonoBehaviour {
public GameObject packMesh;
public float cooldown;
private bool onCooldown;
private AudioSource audioSrc;
public void Start()
{
audioSrc = GetComponent<AudioSource>();
}
void OnTriggerEnter(Collider other)
{
if (!onCooldown)
{
onCooldown = true; //Set Cooldown
packMesh.SetActive(false); //Deactivate mesh
audioSrc.Play();
Hitpoints hpScript = other.GetComponent<Hitpoints>();
PowerUpCountDown countdown = other.GetComponent<PowerUpCountDown>();
countdown.CountDownTimer(30);
//if the clock is still running do PowerUp
if (countdown.hasPowerUp)
hpScript.NewDoubleShield();
Invoke("ReActivate", cooldown); //Re-enable after Cooldown
}
}
private void ReActivate()
{
onCooldown = false;
packMesh.SetActive(true);
}
}
I am still new to programming, and know it is not very pretty how I did it, but eager to learn :)
I don't think you can write it seperately, if you need to use ontriggerenter, all your objects will need the script. There is no way around it.
Your answer
Follow this Question
Related Questions
Anyone wanna help? 0 Answers
RequireComponent not adding in components 1 Answer
FixedUpdate logic step by step 1 Answer
Why won't Time.timeScale set to 0 when I achieve the conditions? 1 Answer
How can I add an article to a noun in this C# code? 0 Answers