Reputation: 179
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:
Also, my Player class:
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)
Upvotes: 0
Views: 60
Reputation: 176
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
Upvotes: 1
Reputation: 179
Turns out it was these lines in my HealthPickup class:
public static int randX = Convert.ToInt32(new Random(1));
public static int randY = Convert.ToInt32(new Random(1));
public static Point Pos = new Point(randX, randY);
Apparently Point() doesn't like random numbers so I changed it to this:
public static Point Pos = new Point(50, 50);
Upvotes: 0