Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9160175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:40:29+00:00 2026-06-17T13:40:29+00:00

I am currently working on a game in which players can destruct the terrain.

  • 0

I am currently working on a game in which players can destruct the terrain. Unfortunately, I am getting this exception after using the SetData method on my terrain texture:

You may not call SetData on a resource while it is actively set on the
GraphicsDevice. Unset it from the device before calling SetData.

Now, before anyone says that there are other topics on this problem, I have looked at all of those. They all say to make sure not to call the method within Draw(), but I only use it in Update() anyways. Here is the code I am currently using to destruct the terrain:

public class Terrain
{
    private Texture2D Image;


    public Rectangle Bounds { get; protected set; }

    public Terrain(ContentManager Content)
    {
        Image = Content.Load<Texture2D>("Terrain");
        Bounds = new Rectangle(0, 400, Image.Width, Image.Height);
    }

    public void Draw(SpriteBatch spriteBatch)
    {

        spriteBatch.Draw(Image, Bounds, Color.White);


    }

    public void Update()
    {
        if (Globals.newState.LeftButton == ButtonState.Pressed)
        {
            Point mousePosition = new Point(Globals.newState.X, Globals.newState.Y);


            if(Bounds.Contains(mousePosition))
            {
                Color[] imageData = new Color[Image.Width * Image.Height];
                Image.GetData(imageData);

                for (int i = 0; i < imageData.Length; i++)
                {
                    if (Vector2.Distance(new Vector2(mousePosition.X, mousePosition.Y), GetPositionOfTextureData(i, imageData)) < 20)
                    {
                        imageData[i] = Color.Transparent;

                    }
                } 
            Image.SetData(imageData);   
            }
        }
    }

    private Vector2 GetPositionOfTextureData(int index, Color[] colorData)
    {
        float x = 0;
        float y = 0;



        x = index % 800;
        y = (index - x) / 800;

        return new Vector2(x + Bounds.X, y + Bounds.Y);
    }
}

}

Whenever the mouse clicks on the terrain, I want to change all pixels in the image within a 20 pixel radius to become transparent. All GetPositionOfTextureData() does is return a Vector2 containing the position of a pixel within the texture data.

All help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:40:31+00:00Added an answer on June 17, 2026 at 1:40 pm

    You must unbind your texture from the GraphicsDevice by calling:

      graphicsDevice.Textures[0] = null;
    

    before trying to write to it by SetData.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a small personal multiplayer game project. I am using
I am currently working on designing my first FPS game using JOGL. (Java bindings
Salut, I'm currently working on a pool game in flash, using box2d for physics
Background: I am currently working on a game, in which there will be several
I am currently working on creating an import-based pipeline for my indie game using
I am a hobbyist C++ programmer and currently working on a game (using Ogre3D)
I am currently working on a Poker java game exercise, and I get this
I'm currently working on a little text-based game after watching the new Tron movie
I'm currently working on a game, and just a while ago i started getting
I'm working on a game for iPhone which creates a path after your character

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.