Random Texture change between Random Time

Hello guys,

I’m trying to make a random texture change between random time, look up on the internet lot of people used Random.Range, but the problem is to used Random.Range it need to be “float”. How do I used Random.Range for Texture; and for the time I think will be using “yield WaitforSecond”.

as for now this is what I got, the texture change but it flikers:

#pragma implicit
#pragma downcast
#pragma strict

var minWait : float;
var maxWait : float;

var minLook : float;
var maxLook: float;

var look : Texture;
var notLook : Texture;

var alert : boolean;

function Update () 
{
	Guard();
	Now();
}

function Now()
{
	yield WaitForSeconds(Random.Range(minWait, maxWait));
	
	alert = true;
}

function Guard()
{
	if(alert == true)
	{
		//yield WaitForSeconds(Random.Range(minWait, maxWait));
		renderer.material.mainTexture = look;
		alert = false;
	}
	
	if(alert == false)
	{
		yield WaitForSeconds(Random.Range(minLook, maxLook));
		renderer.material.mainTexture = notLook;		
	}
}

bump

Use Random.Range to get a float then use Mathf.Round to round it to a whole number. Next use a switch statement to determine the texture.

Example:

private var randomTex : int;
// Say i have 10 texture
var maxTex : int = 2;

// Our texture here
var tex1 : Texture;
var tex2 : Texture;

function Start()
{
	randomTex = Random.Range(1,maxTex);
	SwitchTex(randomTex);
}

function SwitchTex(tex : int)
{
	switch(tex)
	{
		case 1:
			// Change texture code here
			renderer.material.SetTexture("_MainTex", tex1);
			break;
		
		case 2:
			// Change another texture code here
			renderer.material.SetTexture("_MainTex", tex2);
			break;
	}
}

Something like this. :smile:

Thanks zine92, I will look up your script.

What I’m doing now is a mini game for my “storybook apps”. The mini game is about you need to cross to the safety area, theres guard watching. you need to walk when the guard not looking and hide when the guard watching.

So for the guard, I just used 2 texture, one for watching one for not watching. This change of texture will happen randomly between time.

As for now I manage to make what I want(give and take, need more tweeking). What i did is this:

The first code is for walking the character, to walk I just need to push one button and if let got of the button you will stop.

#pragma implicit
#pragma downcast
#pragma strict

var hit : RaycastHit;
var moveSpeed : int;
var character : GameObject;

static var walking : boolean;

function Update () 
{
	Walking();
	Debug.Log(walking);
}

function Walking()
{
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);	

	if (Input.GetMouseButton(0)) //Returns true during the frame the user touches the object
	{
		if (Physics.Raycast (ray, hit, 100)) 
		{	
			if(hit.collider.tag == "01") 
			{
				walking = true;
			
				speedToMove = moveSpeed * Time.deltaTime;
				character.transform.Translate(Vector3.right * speedToMove, Space.World);	
			}
			else
			{
				walking = false;
			}
		}
	}
}

Second, the code is to randomly change texture for the guard.

#pragma implicit
#pragma downcast
#pragma strict

var minWait : float;
var maxWait : float;

var minLook : float;
var maxLook: float;

var look : Texture;
var notLook : Texture;

function Update() 
{
	Guard();
}

function Guard()
{
	if(CharacterWalk.walking == true)
	{
		yield WaitForSeconds(Random.Range(minWait, maxWait));
		renderer.material.mainTexture = look;
		
		CharacterWalk.walking = false;
	}
	
	if(CharacterWalk.walking == false)
	{
		yield WaitForSeconds(Random.Range(minLook, maxLook));
		renderer.material.mainTexture = notLook;		
	}
}

manage to do all this, but found a problem. In the first cede, I wrote this one line

else
			{
				walking = false;
			}

it doesnt execute, the bool stuck at “True” doenst return to “False” back. Why is this happening.

bump

For the record Random.Range can return an int

About the current problem, assuming you’ve checked that it never goes into the else then obviously it’s always hitting something with a tag “01”. Can’t really know why without looking what your scene is like. Maybe you need a mask.

-edit-

priceap makes a very good point I didn’t think of. Listen to him!

the logic is saying “if the mouse is held down, then do a raycast. If the raycast hits something, then check if it has a tag of “01”, if the tag is not 01, then set “walking” to false”

if you are finding that walking is never being set to false, then it must be that if you click anywhere that is not on the object with the “01” tag, then the raycast is not hitting anything at all, and so never returns true in order to check the value of the tag.

Try making the walking = false statement as an else statement of the raycast, rather than of the tag condition, and see if that works.

Also, instead of using Debug.Log in your update(), you could use it in places in your conditional statements so you not only see if the value changes but whether the conditions before it are being reached at all.

@tertle @priceap,

hello, Thanks for the assist. I manage to make the statment went to “False” back but seems have some other trouble.
The texture keep on flikering after the value have return to “False”.

I’ve change the code so from two seperate code to one code:

// CharacterWalk
// (C) 2011 Hizral Ashraq Zakaria

#pragma implicit
#pragma downcast
#pragma strict

var hit : RaycastHit;
var moveSpeed : int;
var character : GameObject;

var minWait : float;
var maxWait : float;

var look : Texture;
var notLook : Texture;

var kakRos : GameObject;

var walking : boolean;

function Update () 
{
	Walking();
	Guard();
	Debug.Log(walking);
}

function Walking()
{
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);	

	if (Input.GetMouseButton(0)) //Returns true during the frame the user touches the object
	{
		if (Physics.Raycast (ray, hit, 100)) 
		{	
			if(hit.collider.tag == "01") 
			{
				walking = true;
			
				speedToMove = moveSpeed * Time.deltaTime;
				character.transform.Translate(Vector3.right * speedToMove, Space.World);	
			}
		}
	}
}

function Guard()
{
	if(walking == true)
	{
		yield WaitForSeconds(Random.Range(minWait, maxWait));
		kakRos.renderer.material.mainTexture = look;
		yield WaitForSeconds(3);
		walking = false;	
	}	
	
	if(walking == false)
	{
		kakRos.renderer.material.mainTexture = notLook;
	}
}

and here I have a web build to test on. Push on the “Push button” and you can see where my problem is.
http://www.lcgdi.com/Upload/LCGDI%20TEST%20AREA/SampleGameStorybook/WebPlayer/WebPlayer.html

bump

Is it a texure conflict? two textures on the same surface typically causes flickering.

Another way to approach it might be to have two separate cubes… one with each texture, and you could have one move forward, one move back whenever you want them to switch.

Obviously, it should be easier to ensure that the texture is the only one active, but it might be easier to transform location. Or at least it might solve your issue if you don’t get the texture help you need.

ahhhhh, never thought about that. Thanks will do what you suggested.