Nested for loop isnt working
Hi!
I have a "System.Serializable" in which I store information about the diffrent background layers. Now I want to add to them a parallax effect. But it dosn't work as it should be and I don't really understand why.
I have shrinked it down to the bare essentials:
public BackgroundData[] datas;
void Update () {
for (int i = 0; i < datas.Length; i++) {
BackgroundData data = datas [i];
...
if (distanceToCamera < data.spawnDistance) {
...
data.backgrounds.Add (newBack.transform);
}
float addPos = player.rigid.velocity.x * data.speedFactor;
for (int j = 0; j < data.backgrounds.Count; j++) {
data.backgrounds[i].Translate (Vector3.left * addPos * Time.deltaTime);
Debug.Log (data.backgrounds[i].gameObject.name); //<-- always the same
Debug.Log (data.backgrounds.Count); //<-- increase right
}
}
}
}
[System.Serializable]
public class BackgroundData {
[Range(0,1)] public float speedFactor;
...
[HideInInspector]public List<Transform> backgrounds = new List<Transform> ();
}
Comment
Best Answer
Answer by mircojanisch · Jul 29, 2017 at 02:18 PM
Small diffrents can have a huge effect.
I had to change
data.backgrounds[i].Translate (Vector3.left * addPos * Time.deltaTime);
to
data.backgrounds[j].Translate (Vector3.left * addPos * Time.deltaTime);
Now you searching the diffrent? It's the indicator for backgrounds.
Your answer
Follow this Question
Related Questions
how to start counting from the last index the script stop on? 2 Answers
if ALL items in array are something 1 Answer
(SOLVED) Accessing (string) arrays by enumerations from another class 0 Answers
Class array initialization on Start() gives -> Object reference not set to an instance of an object 1 Answer