Custom function calling every other function once

I am trying to build a weather system, where if 12 hours pass every gameObject with a script will have its function called. The script that has the variables passes on a variable, and then the script would process this to change its own according to what was to be changed at that point in time.

So i have a WeatherManager which its going to rain 20mm throughout the day. When the rain starts, call the function accross all scripts in the scene taking the WeatherManager’s 20mm value and process it to identify how much it will fill over 12 hours. Or otherwise just a rate.

A Retainer will then set a rate to refill itself at and gain a variable based on 20mm. And the Retainer will gain over 12 hours 200L of water.

I am uncertain if their is a way to make a function of my own or not. The only way i could make stuff happen as i want is to put everything i want it to do into the Start function. Set Gameobject.Setactive(false) then back to true. And then that function will run.

How could i achive that same thing by just calling it through my manager at a certain point in time?

//Every gameObject with this script.
float tankFill = 0;
float incriment_Rate = 0;

public void WeatherCast(int inputRequest)
{
incriment_Rate = inputRequest;
}

void Update()
{
tankFill += incriment_Rate * Time.deltaTime;
}

//Seperate Script
float outRequest = 3.3f;
if(Time.time >= twelveHourMarkSet)
{
twelveHourMarkSet = Time.time + 12;
global WeatherCast(outRequest);
}

Hello @Talscar ,

Long respond, but easy to follow.

I dont understand exactly what you need, but as i see, you need to call a lot of functions (methods) placed in diferents scripts, that are placed in diferent GameObjects across the scene. Right?

Then, what i think you have to do is:

Create a script called, for example “FillingCalculator”. This script must have all the calculations for all type of objects, in a public metoth
For Example:

 public void CalculateFill()
    {
    if (gameObject.name == pool)
    {
    bla bla
    }    
    else if (gameObject.name == buket) 
    {
    bla bla
    }
    } 

And then, add to this metoth a variable with the quantity it will rain as a required variable to run it:

public void CalculateFill(float QuantityRained, float TimeRained)
  {
    if (gameObject.name == pool)
    {
      Liters = bla bla    QuantitytRained*TimeRained
    }    
    else if (gameObject.name == buket) 
    {
      Liters =  bla bla    QuantitytRained*TimeRained
    }
  } 

So, you should have a lot of objects across the scene, with a script called FillingCalculator that will calculate the liters if you provide them a Quantity and Time rained.

Now you only need to call all that metoths with values, for example CalculateFill(20, 12);

How can easy call them all in once? Easy, make a list or an array with all the objects that need to be calculated. You can create a TAG for all that objects called for example “fillable”, and use this:

GameObject[] ObjectsFillableList = GameObject.FindGameObjectsWithTag("fillable");

foreach (GameObject ObjectToCalculate in ObjectsFillableList)
{
ObjectToCalculate.GetComponent<FillingCalculator>().CalculateFill(20,12);
}

If this don’t help, just ask again! use @tormentoarmagedoom so i will see the notification!

Unfortunatly my i did not think the message was sent because it never finnished submiting. Due to a crash issue i found the resolution i was looking for.

In my script i want to change a variable. I just have a

    [SerializeField]public static float incriment_Rate = 0;

This is a variable i can use and modify. globally in my weather script doing this:

    public float tillRainStateChanges = 15f;
    public float rainTimer;
    public float rain = 5.5f;
    bool isRaining = true;

	void Update ()
    {

        if (Time.time >= rainTimer)
        {
            if (isRaining)
            {
                isRaining = false;
                echonomyLoopStart_Retainer.incriment_Rate = 0;
            }
            else
            {
                isRaining = true;
                echonomyLoopStart_Retainer.incriment_Rate = rain;
            }
            rainTimer = Time.time + tillRainStateChanges;
        }
}

This is s simple resolution i am sorry for wasting anyone’s time but appreciate the effort!

Also avoiding trying to find each gameObject in the scene because it drains a certain level of performance i would like to avoid.