0

I have a few parameters from a different class AnalogGlitch, which I want to assign within this code. Instead of assigning values to them individually, I want to pack them in a list and to this in a loop.

However from the output I can see that the values of the variables to not change at all. I have not used as much C# before and wonder if this is something specific to the language (and not in python for example)?

The code works finde, when I assign values to the variables individually instead of in a loop ...

public class SlenderInView : MonoBehaviour
{
    private AnalogGlitch glitch;

    private bool isLooking = false;

    private List<float> controls = new List<float>();

    void Start()
    {
        glitch = GetComponent<AnalogGlitch>();

        controls.Add(glitch.scanLineJitter);
        controls.Add(glitch.horizontalShake);
        controls.Add(glitch.colorDrift);
    }

    void Update()
    {
        // removed the code dealing with isLooking

        if (isLooking)
        {   
            for(int i=0; i<controls.Count; i++)
            {
                if(controls[i] < 1)
                {
                    controls[i] += (float)0.1;
                }
            }
        }
        else
        {    
            for (int i = 0; i < controls.Count; i++)
            {
                controls[i] += 0;
            }
        }
    }
}

2 Answers 2

3

You're adding the values of the variables to the list - you're not adding the variables themselves. So when you change the content of the list, that won't affect the variables at all. As a simpler example:

int x = 10;
List<int> values = new List<int>();
values.Add(x); // Add the *current value of x* to the list
values[0] = 20; // This doesn't change the value of x
Console.WriteLine(x);

So the behaviour you're seeing is definitely what I'd expect. There's no easy way of achieving the behaviour you want without bigger changes. You could write something like an Int32Holder class, and change the AnalogGlitch variables to be of type Int32Holder, then create a List<Int32Holder>, etc... but I suspect it would be simpler just to hard-code the use of the variables:

glitch.scanLineJitter = MaybeUpdate(glitch.scanLineJitter);
glitch.horizontalShake = MaybeUpdate(glitch.horizontalShake);
glitch.colorDrift = MaybeUpdate(glitch.colorDrift);

Or pass the variables by reference to a method:

MaybeUpdate(ref glitch.scanLineJitter);
MaybeUpdate(ref glitch.horizontalShake);
MaybeUpdate(ref glitch.colorDrift);
Sign up to request clarification or add additional context in comments.

Comments

0

Your variables glitch.scanLineJitter, glitch.horizontalShake and glitch.colorDrift are value types, meaning that when you add them to your list, you actually create a copy of the value they hold. Writing

controls[i] += (float)0.1;

does not change the original variable, it just changes the value contained inside the list, which was a copy of the original.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.