Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SGamerXxX · Aug 14, 2012 at 10:56 PM · texturegraphicsoverlapclear

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")); } }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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");
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SGamerXxX · Aug 16, 2012 at 03:08 AM 0
Share

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.

avatar image OperationDogBird · Aug 16, 2012 at 03:29 AM 0
Share

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.

avatar image SGamerXxX · Aug 16, 2012 at 03:52 AM 0
Share

Sweet that worked thanks alot man.

avatar image OperationDogBird · Aug 16, 2012 at 04:57 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I'm trying to get a picture in game and display it on a plane 2 Answers

use RenderTexture on a sphere correctly? 1 Answer

White lines between blocks in voxel terrain 1 Answer

Virtual Textures 1 Answer

How to disable texture perspective correction? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges