Search code examples
c#xnaxna-4.0

Near-identical code causes errors?


So I have 2 lines of extremely similar code. Here is the first line (which has no problems):

Player.Tex = Content.Load<Texture2D>(@"Textures\d");

And here is the second line (which has a TypeInitializationException)

HealthPickup.Tex = Content.Load<Texture2D>(@"Textures\healthPickup");

In the Player class and the HealthPickup class there is a line just after public class with this:

public static Texture2D Tex;

So why is my HealthPickup class causing this error? Have I missed something obvious? I have tried searching for the exception but nothing helps.

EDIT: As requested, my HealthPickup class is as follows:

http://pastebin.com/RXZ2xh1e

Also, my Player class:

http://pastebin.com/XKuVXPk3

Notice that my HealthPickup class is identical to my Player class apart from 4 variables, (Tex, Dir, Pos and speed in Player and Tex, randX, randY, Pos in HealthPickup)


Solution

  • You are trying to convert an object of type Random to an integer. This is not allowed. You can get a random number from Random object using one of the Next method overloads. Like so :

    (new Random (Guid.NewGuid().GetHashCode())).Next()
    

    using Guid.NewGuid()... is not obligatory, but will make sure you get 2 Random objects with different seeds