I have an array of objects and with the script with its data
that puts them and then I print the array and everything is fine
but when I need any component of the array object it always returns the data of the last
private void newCashiers()
{
for (int i = 0; i < posibleCahsier.Length; i++)
{
GameObject cashier = prefabCashier;
cashier.GetComponent<CashierController>().SetData(prefabName[Random.Range(0, prefabName.Length)]);
print(i+" is create: "+cashier.GetComponent<CashierController>().getInfo());
posibleCahsier[i] = cashier;
print(i + " is create and added: " + posibleCahsier[i].GetComponent<CashierController>().getInfo());
}
for (int i = 0; i < 3; i++) // HERE I WANT TO PRINT THE DATA OF EACH SCRIPT OF THE OBJECT AND ALWAYS PRINT THE DATA THAT HAS BEEN GENERATED IN THE LAST
{
GameObject test = Instantiate(posibleCahsier[cashierSelected]);
test.name = "Cashier " + i;
print(i + ": " + test.GetComponent<CashierController>().getInfo());
}
}
I dont understand what you are trying to do.
In your first loop you asign a cashier object, which is the prefab. You then get a component from the prefab and set some data. Are you sure you dont intend to instantiate there? You are simply overwriting this value posibleCahsier.Length many times. The object you are doing this to remains the same.
So every single entry in posibleCahsier is the same.
I believe you are misunderstanding object references. There is ONE prefab object. In your array you dont save copies of the current state of that object, you save a reference to the object itself. So each and every entry in your array points towards the same object. Everytime you change something about that object, you simply overwrite the one existing object. Every single entry in your array still points to this one singular object you have in the background.
In order to have different states of an object you need to have multiple instances.
Changing something about instance one wont affect instance two or three.
I dont know how you constructed your objects. As i understand it, you want to assign a different, randomly chosen, prefab to each of your cashiers. It depends a bit on how you implemented the SetData method, or rather when you actually make use of the things you set there.
Usually you do something like:
Have one Prefab. You usually never want to change anything about the prefab at runtime.
When you need an object instance of the prefab, Instantiate the prefab
You may now change things about the instance (and others) as you wish
So you basically want to fill your list of possible cashiers with instances of your prefab. After instantiating change the instance as you wish. Instead of instantiating in the second loop you simply chose the corresponding, already instantiated, cashier.
As of now all your cashiers are random, so currently the whole list part and chosing one is kinda pointless, but i assume that is going to change in the future. Otherwise you may just generate a new random one when you need it.
Oh thanks so much. I thought that putting the data in the prefab and saving it in the array would each preserve its data, but I see that I am only modifying the prefab in all of them. Thanks.