- Home /
Component not being accessed in ArrayList
Ok, Here is my problem:
I have 2 classes, GameManager and DrumAnimation.
DrumAnimation is setting a Color variable called Col2 to be private with a public set method.
GameManager has an array of GameObjects with the DrumAnimation Component attached to it. In the coroutine to start a new level. One of the GameObjects in the array is instantiated in an ArrayList. All the GameObjects in the original array are then destroyed and the array is reinstantiated. The last added GameObject in the ArrayList then has it's DrumAnimation component's Col2 set to green.
The problem is that the color of Col2 stays the same when the game is played. Can anybody give me some insight on why this is happening?
@$$anonymous$$Watts
public class DrumAnimation : $$anonymous$$onoBehaviour { Color col2; public Color Col2 { set { col2 = value; } } }
public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour { public GameObject drumPrefab; private GameObject[] drumChoices; private List band$$anonymous$$ates;
void Start() {
drumChoices = new GameObject[4];
band$$anonymous$$ates = new List<GameObject>();
IEnumerator NewLevel()
{
band$$anonymous$$ates.Add((GameObject)Instantiate(drumChoices[index]));
if (band$$anonymous$$ates[band$$anonymous$$ates.Count - 1].GetComponent<DrumAnimation>() != null)
{
band$$anonymous$$ates[band$$anonymous$$ates.Count - 1].GetComponent<DrumAnimation>().Col2 = Color.green;
}
else { Debug.Log("DrumAnimation not found"); }
for(int i = 0; i <drumChoices.Length; i++)
{
Destroy(drumChoices[i]);
}
for(int i = 0; i< band$$anonymous$$ates.Count; i++)
{
band$$anonymous$$ates[i].transform.Translate(0, 2f, 0, Space.Self);
}
drumChoices = new GameObject[4];
}
maybe it is a serialization issue try add [SerializeField] to the private col2 var and see if that helps.
Answer by chomps32 · Sep 09, 2016 at 04:07 PM
Found the problem to be a collision between setting the color after initialization in the GameManager class and setting the color in the Start() method in the DrumAnimation component class. So, I stopped setting the color in the Start() method and it worked just fine. Thanks for your help @DavidWatts