- Home /
How can I call the Load method and/or the ShootingSettings method also only once in the Update ?
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[i].launchForce = launchForce;
shooters[i].automaticFire = automaticFire;
shooters[i].bulletDestructionTime = bulletDestructionTime;
shooters[i].maxDrag = maxDrag;
shooters[i].bulletSpeed = bulletSpeed;
shooters[i].bulletsSlowDown = bulletsSlowDown;
shooters[i].overAllSlowdown = overAllSlowdown;
shooters[i].slowdownAll = slowdownAll;
}
}
}
private void Save()
{
if (shooters.Count > 0)
{
for (int i = 0; i < shooters.Count; i++)
{
PlayerPrefs.SetFloat("Launch Force", shooters[i].launchForce);
SetBool("Automatic Fire", shooters[i].automaticFire);
PlayerPrefs.SetFloat("Bullet Destruction Time", shooters[i].bulletDestructionTime);
PlayerPrefs.SetFloat("Max Drag", shooters[i].maxDrag);
PlayerPrefs.SetFloat("Bullet Speed", shooters[i].bulletSpeed);
SetBool("Bullets Slowdown", shooters[i].bulletsSlowDown);
SetBool("Overall Slowdown", shooters[i].overAllSlowdown);
PlayerPrefs.SetFloat("Slowdown All", shooters[i].slowdownAll);
PlayerPrefs.Save();
}
}
}
private void Load()
{
for (int i = 0; i < shooters.Count; i++)
{
shooters[i].launchForce = PlayerPrefs.GetFloat("Launch Force");
shooters[i].automaticFire = GetBool("Automatic Fire");
shooters[i].bulletDestructionTime = PlayerPrefs.GetFloat("Bullet Destruction Time");
shooters[i].maxDrag = PlayerPrefs.GetFloat("Max Drag");
shooters[i].bulletSpeed = PlayerPrefs.GetFloat("Bullet Speed");
shooters[i].bulletsSlowDown = GetBool("Bullets Slowdown");
shooters[i].overAllSlowdown = GetBool("Overall Slowdown");
shooters[i].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;
}
}
Answer by DiegoSLTS · May 06, 2019 at 03:14 AM
You can use an extra bool variable set to true after the "Load" is called the first time, then check if it's false before calling it, and then set it to false when you set takeControl to true (so it works again the next time).
But that's a bad idea because the thing you're trying to do is not a good design. The whole point of Update is that you do things every frame (or most frames), if you want something to happen once then don't do it inside Update. You can, for example, make "takeControl" a property with a Setter and a Getter. When takeControl is set to false you can run your "Load" code, and it'll run only once. Another option would be to keep "takeControl" as it is, and call "Load" right after you set takeControl to false.
If your idea is to change "takeControl" in the editor before pressing Play then check it's value and call Load inside the Start method, which will run only once. And if you want to be able to change the value while in Play mode then a custom editor would be good (you can call "Load" on the object once you detect takeControl changed).
I'd recommend any of those options before adding bool checks to force things inside Update to run only once.
Your answer
Follow this Question
Related Questions
How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers
How can i pick two randomly items from gameobject array ? 1 Answer
Script for a hue rainbow scroll on the material 1 Answer
How can i using a break point if a gameobject have a collider after added to it ? 1 Answer
How can i get all childs from List ? 3 Answers