I have code that uses a random number to determine if an object is special or not. I use this code for each of the 4 objects that reset each so often.
Random rand = new Random(); int i = rand.Next(1, 25); if (i == 1) { thiss.typer = "boulder"; thiss.texture = Content.Load<Texture2D>("rock"); } else if (i == 2) { thiss.typer = "ice"; thiss.texture = Content.Load<Texture2D>("ice"); } else if (i == 3) { thiss.typer = "bomb"; thiss.texture = Content.Load<Texture2D>("bomb"); } else thiss.typer = "normal";
But every time I execute this code, when it creates 4 objects, when it makes one of them special, it makes them special for some reason. Is there something wrong with this code, or will I need to show more of my code to shed light on it?
I can think of two reasons why this could happen.
Firstly, perhaps you accidentally exchange a link between your objects, so it thiss.typerrefers to the same object in all 4 cases.
thiss.typer
-, , , , rand.Next . new Random() , . , .
new Random()
Random, , , . , .