Jump to content
  • Advertisement
Sign in to follow this  
Null_Reference_Exception

models spawning in same position

This topic is 2144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi
I am having a problem I cant work out, and I am looking for some direction. I am trying to draw a list of 20 models and they are being to the screen but they are all being drawn to the game window at the exact same position and I cant see where my code is wrong, 
 
In my enemy class I have
 
public Enemy(Game1 game)
        {
            randX = random.Next(1, 10);
            randY = random.Next(1, 10);
            randZ = random.Next(1, 10);
            enemyPos = new Vector3(randX, randY, randZ);
            enemyColBox = new Box(enemyPos, 3f, 3f, 3f);                       
            enemyModel = new EntityModel(enemyColBox, game.Content.Load<Model>("Models/enemyShip1"), Matrix.Identity *                                                                            Matrix.CreateScale(0.03f), game);
            hit = game.Content.Load<SoundEffect>("Audio/Hit");
            game.space.Add(enemyColBox); 
            game.Components.Add(enemyModel);
            enemyColBox.Tag = enemyModel;
        }
 
 
Game class
 
protected override void LoadContent()
{
     makeEnemies(20);
}
 
public void makeEnemies(int num)
        {
            for (int x = 0; x < num; x++)
            {              
                    enemy.enemyPos = new Vector3(enemy.randX, enemy.randY, enemy.randZ);
                    newEnemies.Add(new Enemy(this)); 
         
                
            }           
        }
Edited by Null_Reference_Exception

Share this post


Link to post
Share on other sites
Advertisement

Have you tried increasing the range of the random positions you generate (say 1 to 1000 instead of 1 to 10)?

 

It could be that they're all getting spawned at different locations, but because the range is so small it looks like they're all in exactly the same spot.

 

Seeding the random number generator is also a good idea.

Share this post


Link to post
Share on other sites

Thanks Guys

 

 

The problem was as gezegond said that I was generating the same seed values each time. 

simple soulotion was to generate a new seed.

 

//instantiate a new seed

private Random random = new Random(Guid.NewGuid().GetHashCode());

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!