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 ghostreddy · Jun 26, 2012 at 09:27 AM · guicolliderontriggerenterguitexturetriggers

show different textures when trigged entered thrice

i have a player ball and a enemy ball, which moves randomly around & when it hits the player the player should loose one of his live....and my prob is can we display three different GUITextures when the enemy ball hits the player for 3 times. Like my below score GUI Texture

 #pragma strict
 
 var score1 : GUITexture;
 var score2 : GUITexture;
 
 function Start()
 {
     score1.enabled = true;
     score2.enabled = false;
 }
 
 function OnTriggerEnter (other : Collider) {
     if(other.gameObject.tag == "Player")
     {
         score1.enabled = false;
         score2.enabled = true;
     }
 }
Comment
Add comment · Show 7
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 AlucardJay · Jun 26, 2012 at 09:37 AM 1
Share

yep, just add a counter to keep track of the currently used material, then cycle through them with a switch-case.

if you don't know about switch-case I can post an answer for you =]

avatar image syclamoth · Jun 26, 2012 at 09:39 AM 0
Share

I'd have just used an array, but a switch can be used to implement more complex behaviour, so that's good too!

avatar image ghostreddy · Jun 26, 2012 at 10:47 AM 0
Share

below is the script :

var GUITex : GUITexture; // This is where you drop the main GUITexture component!

var images : Texture2D[]; // This is an array of images!

var currentImage : int = 0; // The index of the current texture. //Now, whenever the player gets hit, do something like this:

function OnTriggerEnter (other : Collider) {

if(other.gameObject.tag == "Player") { currentImage = $$anonymous$$athf.Clamp(currentImage++, 0, images.Length); // Advances the index! GUITex.texture = images[currentImage]; // Changes the image! }

}

avatar image ghostreddy · Jun 27, 2012 at 07:01 PM 0
Share

@alucardj: thanks for the script its working....since i have two balls in the scene how can i tag them.....i mean now its like when one of the ball hits the player its displaying the data under case 1 & if the other ball hits the player its also displaying the data under case 1? how can i combine the both balls like if one ball hits the player it displays data under case 1 & even if other ball hits the player then it displays the data under case 2.

avatar image AlucardJay · Jun 28, 2012 at 07:01 PM 1
Share

sry, all fixed now. (just wanted to follow up on my switch comment, also maybe wasn't confident enough to post as answer alongside one of the pro's!)

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by syclamoth · Jun 26, 2012 at 09:38 AM

What you have now is a setup where you can only have 2 things- for more, it will have to be a little more complex.

For starters, instead of hardcoding exactly two 'score' images, try keeping a collection of possible textures to switch your GUITexture between:

 var GUITex : GUITexture; // This is where you drop the main GUITexture component!

 var images : Texture2D[]; // This is an array of images!

 private var currentImage : int = 0; // The index of the current texture.

Now, whenever the player gets hit, do something like this:

 currentImage = Mathf.Clamp(currentImage++, 0, images.Length); // Advances the index!
 GUITex.texture = images[currentImage]; // Changes the image!

This will move the image forward every time the player enters the trigger, if you use it in the same way you were using your old code.

Comment
Add comment · Show 3 · 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 ghostreddy · Jun 26, 2012 at 10:06 AM 0
Share

$$anonymous$$ieran Wallace: thanks for the script....but its updating only once i mean even though we have added three textures its displaying only one.

avatar image ghostreddy · Jun 26, 2012 at 10:36 AM 0
Share

Below is the code that i have used #pragma strict

var GUITex : GUITexture; // This is where you drop the main GUITexture component!

var images : Texture2D[]; // This is an array of images!

var currentImage : int = 0; // The index of the current texture. //Now, whenever the player gets hit, do something like this:

function OnTriggerEnter (other : Collider) {

if(other.gameObject.tag == "Player") { currentImage = $$anonymous$$athf.Clamp(currentImage++, 0, images.Length); // Advances the index! GUITex.texture = images[currentImage]; // Changes the image! }

}

avatar image ghostreddy · Jun 26, 2012 at 10:51 AM 0
Share

can you pls suggest me the same script using switch case.

Thanks!!!

avatar image
0

Answer by AlucardJay · Jun 28, 2012 at 06:42 PM

this is just an example of a switch-case, I am still beginning (and syclamoth's answer is very logical and compact).

 private var currentImage : int = 0;

 function OnTriggerEnter (other : Collider) {
     if(other.gameObject.tag == "Something")
     {
         currentImage++;
         switch(currentImage)
         {
             case 1 :
                 // do stuff for condition 1, eg tex = myTex1; 
                 // and/or play a sound, eg audio.Play();
                 // or call a function, more than one thing can be added here, and it's better than clustered if's
             break;
             
             case 2 :
                 // do stuff for condition 2
             break;
             
             case 3 :
                 // do stuff for condition 3
                 
                 // * end of the count of 3 is reached
                 // make image stay on case 3 (currentImage++; at start of trigger event makes 3),
                 // or just let the switch fall to default
                 currentImage = 2;
                 // or, cycle through again with
                 // currentImage = 0;
             break;
             
             default :
                 // have default settings if currentImage counter is under 1 or over 3, or see checks in case 3
                 // or do nothing and leave this part empty with just //
             break;
         }
     }
 }
Comment
Add comment · Show 7 · 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 AlucardJay · Jun 28, 2012 at 06:44 PM 0
Share

ghostreddy : @alucardj: thanks for the script its working....since i have two balls in the scene how can i tag them.....i mean now its like when one of the ball hits the player its displaying the data under case 1 & if the other ball hits the player its also displaying the data under case 1? how can i combine the both balls like if one ball hits the player it displays data under case 1 & even if other ball hits the player then it displays the data under case 2.

avatar image AlucardJay · Jun 28, 2012 at 06:45 PM 0
Share

you can get all sorts of information from the collider. my last example used :

 if (other.gameObject.tag == "Something")

this can also be done with the GameObject's name :

 if (other.gameObject.name == "Ball_1")

So each if statement and switch-case needs a counter, something like this :

 private var counter1 : int = 0;
 private var counter2 : int = 0;

 function OnTriggerEnter (other : Collider) {

     if (other.gameObject.name == "Ball_1")
     {
        counter1++;
        switch(counter1)
        {
          case 1 :
           // 
          break;

          ....
        }
     }
     
     if (other.gameObject.name == "Ball_2")
     {
        counter2++;
        switch(counter2)
        {
          case 1 :
           // 
          break;

          ....
        }
     }
 }
avatar image ghostreddy · Jun 28, 2012 at 09:17 PM 0
Share

@$$anonymous$$ $$anonymous$$ay @alucardj: If we have applied below script even then it leads us to the same prob i guess....even though we are classifying the balls as 1 and 2 but if player hits ball1 code under case 1 will be loaded & when the player hits ball2 then also the code under case 1 will be loaded. here we need to tag both the balls like if player hits ball 1 then code under case1 should be executed and when the player hits ball2 or ball1 then case2 should be executed.

avatar image AlucardJay · Jun 29, 2012 at 05:22 AM 0
Share

to make the player affected by either ball, you can have both Ball_1 and Ball_2 tagged as 'Ball', then just use :

 if (other.gameObject.tag == "Ball")

or check for each object name in the conditional :

 if (other.gameObject.name == "Ball_1" || other.gameObject.name == "Ball_2")
avatar image ghostreddy · Jun 30, 2012 at 08:25 AM 0
Share

@alucardj: its working thanks.......is it possible to do something like this:

Since the enemy ball move randomly around the scene if enemy balls co$$anonymous$$g closure to player ball then we need to suggest the safe direction to the player ball by displaying the guitexture arrows like left,right,up,down like that......Eg: if enemy balls are moving closure to player ball in +z axis then the arrow should suggest the safe direction either left or right.

Show more comments

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

6 People are following this question.

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

Related Questions

why is one side of collider behaving different than the other 2 Answers

percentage health bar 1 Answer

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

Input.GetKeyDown and OnTriggerEnter problem. 2 Answers

Please HELP: ¿How can i made a drag and drop quiz?, i got some problems whit my scripts, like connecting the drag and drop function whit the buttons 0 Answers


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