- Home /
Trying to switch Textures on object based on timer
Ok so I have done a complete revamp to the script including all of the changes made via suggestions in this question and another I have. The new and improved script is below. The only thing I think I need at this point to get it working is how do I call the textures in the array to replace First Texture, Second Texture, Third Texture, and Fourth Texture?
var textureArray : Texture2D[];
var damageAmount = int;
var healAmount = int;
var ammoAmount = int;
var pointAmount = int;
var damageDelay : float = 1;
var healDelay : float = 1;
var interval:float = 10;
var myTimer: float = 10;
private var nextDamageAllowed : float;
private var nextHealAllowed : float;
private var nextAmmoAllowed : float;
private var nextPointAllowed : float;
function Update ()
{
if(myTimer > 0)
{
myTimer -= Time.deltaTime;
}
if(myTimer <= 0)
{
randomNumber = Random.Range(0, textureArray.length);
renderer.material.mainTexture = textureArray[randomNumber];
myTimer += interval;
}
}
//When player enters trigger hurt them if the hurtTexture is active
function OnTriggerEnter(thecollision : Collider)
{
if(theCollision.gameObject.tag == "Player" && First Texture)
{
GiveDamage;
} else if (theCollision.gameObject.tag == "Player" && Second Texture) {
//when player enters trigger heal them if the healTexture is active
GiveHeal;
} else if (theCollision.gameObject.tag == "Player" && Third Texture) {
//when player enters trigger give them ammo if the AmmoTexture is active
GiveAmmo;
} else if (theCollision.gameObject.tag == "Player" && Fourth Texture) {
//when player enters trigger give them points if the PointTexture is active
GivePoints;
}
}
//when player stands on trigger hurt them periodically if the HurtTexture is active
function OnTriggerStay(thecollision : Collider)
{
if (theCollision.gameObject.tag == "Player" && First Texture)
{
GivePeriodicDamage;
} else if (theCollision.gameObject.tag == "Player" && Second Texture) {
//when player stands on trigger heal them periodically if the healTexture is active
GivePeriodicHeal;
} else if (theCollision.gameObject.tag == "Player" && Third Texture) {
//when player stands on trigger give them ammo periodically if the AmmoTexture is active
GivePeriodicAmmo;
} else if (theCollision.gameObject.tag == "Player" && Fourth Texture) {
//when player stands on trigger give them points periodically if the PointTexture is active
GivePeriodicPoints;
}
}
//Function to damage player for OnTriggerEnter
function GiveDamage()
{
SendMessage("ApplyDamage", damageAmount, SendMessageOptions, DontRequireReceiver);
}
//Function to heal player for OnTriggerenter
function GiveHeal()
{
SendMessage("Heal", healAmount, SendMessageOptions, DontRequireReceiver);
}
//Function to give ammo to player for OnTriggerEnter
function GiveAmmo()
{
gunScript.clips += ammoAmount;
}
//Function to give points to player for OnTriggerEnter
function GivePoints()
{
PointManager.points += pointAmount;
}
//Function to hurt player for OnTriggerstay
function GivePeriodicDamage()
{
if(Time.time > nextDamageAllowed)
{
SendMessage("ApplyDamage", damageAmount, SendMessageOptions, DontRequireReceiver);
nextDamageAllowed = Time.time + damageDelay;
}
}
//Function to heal player for OnTriggerStay
function GivePeriodicHeal()
{
if(Time.time > nextHealAllowed)
{
SendMessage("Heal", healAmount, SendMessageOptions, DontRequireReceiver);
nextHealAllowed = Time.time + healDelay;
}
}
//Function to give ammo to player for OnTriggerStay
function GivePeriodicAmmo()
{
if(Time.time > nextAmmoAllowed)
{
gunScript.clips += ammoAmount;
nextAmmoAllowed = Time.time + ammoDelay;
}
}
//Function to give points to player for OnTriggerStay
function GivePeriodicPoints()
{
if(Time.time > nextPointAllowed)
{
PointManager.points += pointAmount;
nextPointAllowed = Time.time + pointDelay;
}
}
Ok I have posted the full script that the original portion belongs to in the original post (for formating reasons). I need to know how to replace HurtTexture, HealTexture, AmmoTexture, and PointTexture with the textures in the array since I no longer have functions for them...
Answer by Piflik · Sep 11, 2011 at 09:53 PM
Well...you need to add 'break;' after each 'case' line, or it will just run through all 4 cases and display the last one. Also, the upper limit of Random.Range is excluded, so you will have to use a 5 there instead of 4.
Reset() would be just 'myTimer = 10.0' if I'm not missing something obvious. Wouldn't even need a function, you could add this line simply to your 'if(myTimer
A little addendum: personally I think FixedUpdate() is better suited for counters than Update().
Thanks for the input I have edited the script quite a bit and got it working to a certain point. And just curious are you sure that the 4 won't count in random.range? currently the above script worked (to a point) but it only ever displayed the texture in case 4 spot. I have also used random 1,3 in another game that generates 3 types of loot with no problem.. did I accidently find some loophole in the other script or something?
Not saying you are wrong, don't misunderstand me, just trying to better understand the feature.
Yeah, I am sure. Random.Range(1, 4) will only ever generate 1, 2 or 3, never 4. It is displaying material 4 in your original script, because you were missing the 'break;' after each 'case'. It actually displayed all of them in turn, but too fast for you to see it until it settled for the last one.
Answer by Waz · Sep 11, 2011 at 08:57 PM
Random.Range(1,4) never returns 4.
switch cases should end in a break statement.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How can i delete an item from an array after being used? (So, it won't be repeated) 2 Answers
Changing Objects Material Using My Array 1 Answer
problem with FindWithTag 2 Answers
Random textures... 1 Answer