Finding a way to fit in an "If" statement

Hi! :smile:

I’m having a little problem here,
This is a script I am using on a model to switch through 3 different textures:

// Assign the texture exposed in the inspector the renderer's material
var textures : Texture[];

// When you click on the object
function OnMouseDown() { 

    // we want this texture index now
    var index : int = Time.time;
    
    // take a modulo with size so that animation repeats
    index = index % textures.length;
    
    // assign it
    renderer.material.mainTexture = textures[index];
}

In The inspector it looks like:
[![[/url]

This part is really working great, and it does what I want. (Change the texture each time I click on it.)
* ps: the black lines are in place to keep the name a secret :sweat_smile: (upcomming project) :wink:

Now I have a little problem, I’m trying to add some functions into the script. For an example on what I mean I will just take this piece:

Destroy(gameObject);

So if I get to texture Element 1 I want this to happen using an “if” statement.
When I get to texture Element 2 I want another thing to happen etc.

Does anyone know what variables to use to achieve this result? I pretty much tried the last 4 hours getting this to work :frowning:

Thank You!

This part is really working great, and it does what I want. (Change the texture each time I click on it.)
* ps: the black lines are in place to keep the name a secret :sweat_smile: (upcomming project) :wink:

Now I have a little problem, I’m trying to add some functions into the script. For an example on what I mean I will just take this piece:

§_DISCOURSE_HOISTED_CODE_1_§

So if I get to texture Element 1 I want this to happen using an “if” statement.
When I get to texture Element 2 I want another thing to happen etc.

Does anyone know what variables to use to achieve this result? I pretty much tried the last 4 hours getting this to work :frowning:

Thank You!

// Assign the texture exposed in the inspector the renderer's material 
var textures : Texture[]; 

// When you click on the object 
function OnMouseDown() { 

    // we want this texture index now 
    var index : int = Time.time; 
    
    // take a modulo with size so that animation repeats 
    index = index % textures.length; 
    
    // assign it 
    renderer.material.mainTexture = textures[index];

    if(index == 0) {
       // do something
    } else if (index == 1) {
       // do something else
    } else {
       // do a third thing
    }
}

If you have too many conditions then use a switch statement.
Perhaps I don’t understand what you are asking exactly?

You understand :smile:
At least I think you do because your script modification gives me the exact thing I was going for! Thank you! :smile:

But now to build on further with the script, I’ve made this

if(index == 2) { 
yield WaitForSeconds(0.50); 
index -= 2;
print ("Test if it is working");
}

It is supposed to wait for 0.5 seconds, and then decrease the index by 2, bringing it back to index 0.

It does “print” the text Test if it is working but the index doesn’t goes back to image 0.

Is my code for this maybe wrong?

index -= 2;

Thank You!

  • Tim

EDIT:
To fully explain this,
I want to be able that if I reach texture number 2,
another object, with the following script attached, stops moving when it is in close proximity of the object with the changing textures.

var targetA : GameObject;
var targetB : GameObject;
var speed : float = 0.1;

function FixedUpdate () {
	var weight = Mathf.Cos(Time.time * speed * 2 * Mathf.PI) * 0.5 + 0.5;
	transform.position = targetA.transform.position * weight
						+ targetB.transform.position * (1-weight);
}

Maybe you will understand it better now :wink:
(And maybe know the script to pause or freeze this script?)

Thank You!:smile:

  • Tim