This script will global control over all the Shooting scripts.
The idea is to be able to change the global variables in real time while the game is running.
The problem is when I’m calling the Load method from the Update since it’s calling it nonstop then I can’t change anymore each Shooting script settings. I want to be able to takeControl if it’s true then use this ShootingManager script but if takeControl is false make Load once so I will be able to change the settings if I want on each of the Shooting scripts. But since Load is in the Update and takeControl is false it will keep Loading the old settings nonstop so I can’t change any settings in the Shooting script/s while the game is running.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ShootingManager : MonoBehaviour
{
[Header("Main")]
public float launchForce = 700f;
public bool automaticFire = false;
public float bulletDestructionTime;
public bool takeControl = false;
[Space(5)]
[Header("Slow Down")]
public float maxDrag;
public float bulletSpeed;
public bool bulletsSlowDown = false;
public bool overAllSlowdown = false;
[Range(0, 1f)]
public float slowdownAll = 1f;
private List<Shooting> shooters;
// Start is called before the first frame update
void Start()
{
shooters = FindObjectsOfType<Shooting>().ToList();
Save();
if (takeControl == true)
{
ShootingSettings();
}
}
// Update is called once per frame
void Update()
{
if (takeControl == true)
{
ShootingSettings();
}
else
{
Load();
}
}
private void ShootingSettings()
{
if (shooters.Count > 0)
{
for (int i = 0; i < shooters.Count; i++)
{
shooters*.launchForce = launchForce;*
shooters*.automaticFire = automaticFire;*
shooters*.bulletDestructionTime = bulletDestructionTime;*
shooters*.maxDrag = maxDrag;*
shooters*.bulletSpeed = bulletSpeed;*
shooters*.bulletsSlowDown = bulletsSlowDown;*
shooters*.overAllSlowdown = overAllSlowdown;*
shooters*.slowdownAll = slowdownAll;*
}
}
}
private void Save()
{
if (shooters.Count > 0)
{
for (int i = 0; i < shooters.Count; i++)
{
PlayerPrefs.SetFloat(“Launch Force”, shooters*.launchForce);*
SetBool(“Automatic Fire”, shooters*.automaticFire);*
PlayerPrefs.SetFloat(“Bullet Destruction Time”, shooters*.bulletDestructionTime);*
PlayerPrefs.SetFloat(“Max Drag”, shooters*.maxDrag);*
PlayerPrefs.SetFloat(“Bullet Speed”, shooters*.bulletSpeed);*
SetBool(“Bullets Slowdown”, shooters*.bulletsSlowDown);*
SetBool(“Overall Slowdown”, shooters*.overAllSlowdown);*
PlayerPrefs.SetFloat(“Slowdown All”, shooters*.slowdownAll);*
PlayerPrefs.Save();
}
}
}
private void Load()
{
for (int i = 0; i < shooters.Count; i++)
{
shooters*.launchForce = PlayerPrefs.GetFloat(“Launch Force”);*
shooters*.automaticFire = GetBool(“Automatic Fire”);*
shooters*.bulletDestructionTime = PlayerPrefs.GetFloat(“Bullet Destruction Time”);*
shooters*.maxDrag = PlayerPrefs.GetFloat(“Max Drag”);*
shooters*.bulletSpeed = PlayerPrefs.GetFloat(“Bullet Speed”);*
shooters*.bulletsSlowDown = GetBool(“Bullets Slowdown”);*
shooters*.overAllSlowdown = GetBool(“Overall Slowdown”);*
shooters*.slowdownAll = PlayerPrefs.GetFloat(“Slowdown All”);*
}
}
public void SetBool(string name, bool booleanValue)
{
PlayerPrefs.SetInt(name, booleanValue ? 1 : 0);
}
public bool GetBool(string name)
{
return PlayerPrefs.GetInt(name) == 1 ? true : false;
}
public bool GetBool(string name, bool defaultValue)
{
if (PlayerPrefs.HasKey(name))
{
return GetBool(name);
}
return defaultValue;
}
}