- Home /
How to clear previous Graphics.DrawTexture?
I am using Graphics.DrawTexture to draw an image, and then when collision occurs draw another image. Problem is that I recently made my background of image transparent, so that the previous images are showing through (and overlapping), Is there a way to "clear" the previous image first before I draw the next one so this doesn't happen?
Note: Destroy command does not work because the image is an asset. This is the code concerned:
var number : int = 100;
var image1 : Texture
var image2 : Texture
function OnGUI() {
Graphics.DrawTexture(Rect(Screen.width - 110, 10, 104, 20), image1);
if(number == 80){
//Destroy(image1);
Graphics.DrawTexture(Rect(Screen.width - 110, 10, 104, 20), image2);
}
Edit: Whole Code
var hitSound : AudioClip;
var number : int = 100; var decreaseNumber : int = 20; var image1 : Texture; var image2 : Texture; var image3 : Texture; var image4 : Texture; var image5 : Texture;
private var currentImage: Texture; private var changed:boolean = false;
function Start(){
currentImage = image1;
}
function OnGUI() {
Graphics.DrawTexture(Rect(Screen.width - 110, 10, 104, 20), currentImage);
if(number == 80){
currentImage = image2;
}
if(number == 60){
currentImage = image3;
}
if(number == 40){
currentImage = image4;
}
if(number == 20){
currentImage = image5;
}
if(number == 0){
Application.LoadLevel ("Level2");
}
}
function OnCollisionEnter(col : Collision){
if(col.gameObject.name == "gameObject1") { number -= decreaseNumber; AudioSource.PlayClipAtPoint(hitSound, transform.position); //Debug.Log(number); Destroy(GameObject.Find("gameObject1")); } }
Answer by OperationDogBird · Aug 15, 2012 at 05:39 AM
So what you want to do is have an array for the textures and use a global index to decide the proper texture.
public var textures:Texture[]; //Make sure to set all 5 textures
public var number : int = 100;
public var decreaseNumber : int = 20
public var hitSound : AudioClip;
private var index:int;
function OnCollisionEnter(col : Collision)
{
if(col.gameObject.name == "gameObject1")
{
index++; //This changes the image
number -= decreaseNumber;
AudioSource.PlayClipAtPoint(hitSound, transform.position);
Destroy(col.gameObject);
}
}
function OnGUI()
{
Graphics.DrawTexture(Rect(Screen.width - 110, 10, 104, 20),textures[index]);
}
function Update()
{
if(number == 0)Application.LoadLevel ("Level2");
}
I haven't quite got it working, I am not sure what you mean by "$$anonymous$$ake sure to set all 5 textures". How do you set them, I can't seem to set them in the inspector if that what you mean. That and I keep getting this weird "Array index of of range" error.
In the inspector there will be an arrow next to the name Textures. Click the arrow, you will see a 0. Click the 0 and type in 5. You will now how 5 available slots for textures. $$anonymous$$ake sure you insert the textures in the order that they should appear.
No problem. Look up the reference for built in arrays and make sure you understand how they work. They are super handy when dealing with more than 2 objects of the same type. Once you understand how to deal with arrays, im sure you will use them a lot and your code will get much cleaner and more efficient.
Your answer
