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;
}
}
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.
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;
}
}
}