Hey guys - thanks for looking at my question.
I'm making a small cardgame. I have a script set up that 'deals' the card at the beginning. It's supposed to 1. place the cards in the start position (for solitaire) and 2. assign a random cardTexture to it.
This works perfectly, but the loop seems to skip the very first card, which gets dealt without any material applied.
I'll walk you through my code:
- depending on number of the card, get instantiated at a certain place
- pick a random element of the cardtypes array (Two of Spades, for instance. It has the same order as the textures array)
- create a material, apply the texture that corresponds to the cardtype
- apply the material to the card and apply the cardtype as the name
- delete the material and cardtype from the arrays so they wont be re-used
All these steps work for all cards, except the very first card (i=0) skips the applying the material step for some reason.. If I check the arrays at the end they are empty though..
function Start () {
for(i=0;i<52;i++){
if(i<23) Instantiate(card,Vector3(0,i*0.01,3),Quaternion.identity);
else if(i<30) Instantiate(card,Vector3((12-2*(i-23)),0.00,-0.0),Quaternion.identity);
else if(i<36) Instantiate(card,Vector3((12-2*(i-30)),0.01,-0.3),Quaternion.identity);
else if(i<41) Instantiate(card,Vector3((12-2*(i-36)),0.02,-0.6),Quaternion.identity);
else if(i<45) Instantiate(card,Vector3((12-2*(i-41)),0.03,-0.9),Quaternion.identity);
else if(i<48) Instantiate(card,Vector3((12-2*(i-45)),0.04,-1.2),Quaternion.identity);
else if(i<50) Instantiate(card,Vector3((12-2*(i-48)),0.05,-1.5),Quaternion.identity);
else if(i<51) Instantiate(card,Vector3((12-2*(i-50)),0.06,-1.8),Quaternion.identity);
//pick a rendom element from the cardtypes array
var randomPick = Random.Range(0,51-i);
//create a material and apply a texture from the cardTextures array - which has the same order as cardtypes. Also rename the card.
var cardMaterial : Material = new Material(Shader.Find("Diffuse Detail"));
cardMaterial.mainTexture = cardTextures[randomPick];
card.renderer.material = cardMaterial;
card.name = ""+cardTypes[randomPick];
//remove the element from the arrays
cardTypes.RemoveAt(randomPick);
cardTextures.RemoveAt(randomPick);
yield new WaitForFixedUpdate ();
}
Debug.Log(""+cardTextures); //this is EMPTY so all textures are selected and deleted, why not applied? :S
}
Many thanks in advance!
You're absolutely right about this, I also caught this but changing it doesn't actually fix it though o.O and when I don't fix the Random.Range the last iteration will pick the final name, so I can then catch it and fix it. So there must be even an other mistake somewhere o.O I'm just going to start over fresh and rethink every step of the way xD thanks though!
– anon73820239You are absolutely genius for spotting these two errors though - I have been spending the entire day going crazy over this stupid script and I didn't spot it until an hour ago.
– anon73820239