Search code examples
c#unity-game-enginelight

How do I store and call a temporary variable in C#


Apologies in advance! Beginner here. I'm finding myself using a feature more and more often like this one I'm going to describe, to store a temporary variable and recalling it. I tried looking it up, but haven't found an answer specifically related to my issue.

I'm trying to do something very simple, once I understand how it's done I can apply it to other types (etc).

Let's say I want to change the color of a light, but store the original light setting in a variable, then recall that setting once the timer has run out. This is for a game in Unity.

if (powerupEnabled)
        {
            CameraShake.instance.shakeDuration = 5;
            powerupTimer -= Time.deltaTime;
            snakeheadFire.SetActive(true);
            //lightIngame.intensity = 0.8f;
            Color original_color = lightIngame.color; // <<<< Trying to store original color set in game in variable
                            
            lightIngame.color = Color.red;
            if (lightIngame != null)
            {
                // add the amount of time that has passed since last frame
                timeElapsed += Time.deltaTime;

                // if the amount of time passed is greater than or equal to the delay
                if (timeElapsed >= delay)
                {
                    // reset the time elapsed
                    timeElapsed = 0;
                    // toggle the light
                    
                    ToggleLight();
                }
            }
            if (powerupTimer <= 0)
            {
                lightIngame.color = original_color; // <<<< Trying to restore original color set in game from variable
                CameraShake.instance.shakeDuration = 0;
                lightIngame.intensity = 1.12f;
                print("Timer stopped!");
                powerupTimer = 5f;
                snakeheadFire.SetActive(false);
                powerupEnabled = false;
            }
        }

So basically I tried Color original_color = lightIngame.color, but once I call original color back, the light won't change to its original setting. The two lines (color changes) are referenced with '<<<<'

What am I missing?


Solution

  • The temp variable is created every frame, if that function runs every frame. You need a variable that can survive until your powerupTimer runs out.

    I suggest this:

    private Color temp_value; // this lives in the class, not in the function.
    void Update()
    {
        if (powerupEnabled)
        {
            // do stuff
    
            if (powerupTimer <= 0)
            {
                setPowerupState(false); // disable powerup
            }
        }
    }
    void setPowerupState(bool enabled)
    {
        powerupEnabled = enabled;
        if(enabled)
            temp_value = lightIngame.color; // store the original color once, not every frame.
        else
            lightIngame.color = temp_value; // restore the saved value.
    }