- Home /
if for array
I have an array that contains 5 textures. I want to make sure that when I click with the mouse on the red texture (array number 4) this object is deleted. If you click when the object has the texture (array number2) it takes away points. My idea was this:
[CODE]
function OnMouseDown(){
if(renderer.material.mainTexture == [1]){ //when the texture is the same array element 1
Destroy(gameObject);
}
}
Answer by Screenhog · Dec 10, 2012 at 11:54 PM
One way would be to use a switch statement. (If you don't know what they are, look them up, they're quite useful.) This code assumes that "texArray" is your array of 5 textures.
function OnMouseDown(){
switch (renderer.material.mainTexture) {
case texArray[0]:
//do something
break;
case texArray[1]:
//do something
break;
case texArray[2]:
//do something to take away points
break;
case texArray[3]:
//do something
break;
case texArray[4]:
Destroy (gameObject);
break;
}
}
This, of course, is just going from what you've said in your description. There may be more efficient ways to do what you want, rather than reading the texture off of an object.
Your answer
Follow this Question
Related Questions
Wearied Error 1 Answer
if a string has certain letter(s) or number(s) 2 Answers
Null Reference Exception After refering to an Array 1 Answer
Exception: not implemented? 2 Answers