Script not working, any help appreciated.

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

UPDATED SCRIPT

var textureArray : Texture2D[] = new Texture2D[4];
var damageAmount = int;
var healAmount = int;
var ammoAmount = int;
var pointAmount = int;
var damageDelay : float = 1;
var healDelay : float = 1;
var ammoDelay : float = 1;
var pointDelay : 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;
   }else {
      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" && textureArray[0])
   {
    GiveDamage();
   } else if (theCollision.gameObject.tag == "Player" && textureArray[1]) {
      //when player enters trigger heal them if the healTexture is active
      GiveHeal();
   } else if (theCollision.gameObject.tag == "Player" && textureArray[2]) {
      //when player enters trigger give them ammo if the AmmoTexture is active
      GiveAmmo();
   } else if (theCollision.gameObject.tag == "Player" && textureArray[3]) {
      //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" && textureArray[0])
    {
       GivePeriodicDamage();
    } else if (theCollision.gameObject.tag == "Player" && textureArray[1]) {
       //when player stands on trigger heal them periodically if the healTexture is active
       GivePeriodicHeal();
    } else if (theCollision.gameObject.tag == "Player" && textureArray[2]) {
      //when player stands on trigger give them ammo periodically if the AmmoTexture is active
      GivePeriodicAmmo();
    } else if (theCollision.gameObject.tag == "Player" && textureArray[3]) {
      //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 += 3;
}

//Function to give points to player for OnTriggerEnter
function GivePoints()
{
   ScoreManager.points += 3;
}

//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 += 3;
     nextAmmoAllowed = Time.time + ammoDelay;
   }
}

//Function to give points to player for OnTriggerStay
function GivePeriodicPoints()
{
   if(Time.time > nextPointAllowed)
   {
     ScoreManager.points += 3;
     nextPointAllowed = Time.time + pointDelay;
  }
}

Are you sure this compiles? It seems like the brackets don’t line up.

Example:

function Update()
{//start update

     if(Time.time >= timer) //this is good
         randomNumer = random.range (1,4);
}//end update

//not part of update, will only be executed once on object creation.
switch(randomNumber){

    case 1: HealTexture;//function is not called: there are no "()"
    //Missing break; statement. Will either execute all cases or not compile
    //skip a little

//not part of update
 timer = Time.time + interval; //resets the timer every call, not only when time has elapsed
           //== time code never finishes; move this inside the randomNumber if statement

do you even need a switch-case and 4 different vars and 4 different functions? Change your four separate vars to an Texture2d with length 4, and then just do this in update:

 renderer.material.mainTexture = textureArray[randomNumber];

And this to create randomNumber:

 randomNumber = Random.Range(0, textureArray.length);

Also, you have a lot of bracket errors on the support functions below.

Try opening your scripts in a third party editor that makes bracket alignment nice and color-coded (like BlueJ?). I’m seeing more syntax and bracket errors than I am logic errors.

You are doing a nice job of creating small support methods for common functions, and I’m loving the idea of your tile game. All you have to do is figure out the syntax and this should work.

About the questions in comments about ‘if’ and ‘else if’:

if(myTimer > 0)
{
    myTimer -= Time.deltaTime;
}

if(myTimer <= 0)
{
   randomNumber = Random.Range(0, textureArray.length);
   renderer.material.mainTexture = textureArray[randomNumber];
   myTimer += interval;
}

Here, you could replace the ‘if (myTimer <= 0)’ with just ‘else’ (without ‘if’). Because if the myTimer is not bigger than 0 it can only be smaller or equal 0 (which is precisely your second statement).

if (theCollision.gameObject.tag == "Player" && First Texture)
{
  GivePeriodicDamage;
} else if (theCollision.gameObject.tag == "Player"  && Second Texture) 
{
  GivePeriodicHeal;
} else if (theCollision.gameObject.tag == "Player" && Third Texture) 
{
  GivePeriodicAmmo;
} else if (theCollision.gameObject.tag == "Player" && Fourth Texture) 
{
  GivePeriodicPoints;
}

Here, everything is mandatory (no redundant ‘else if’) IF there is a possibility that none of all 4 statements are met. e.g. if theCollison.gameObject.tag is not “Player” or there is maybe a fifth Texture or no Texture at all.

Else, if it’s clear that if the first 3 statements are not met, it always has to be the fourth statement, then you could leave out the last if ().

(But I think that’s not the case here)

Example: if theCollision.gameObject.tag can be “not Player”, but it’s always one of the four Textures, you could do this:

if (theCollision.gameObject.tag == "Player")
{
  if (First Texture) 
  {
     GivePeriodicDamage;
  } else if (Second Texture) 
  {
     GivePeriodicHeal;
  } else if (Third Texture) 
  {
     GivePeriodicAmmo;
  } else
  {
     GivePeriodicPoints;
  }
}

Greetz, Ky.

Thinking about SilverTabbys proposal, I’m quite sure it won’t currently work… Right now, your Array is empty, so textureArray.length is 0. So randomNumber is always 0. But if you now take textureArray[randomNumber] it tries to access the Element of textureArray with the Index of [0] (which is the first element of an Array), but there’s nothing there so it will throw an error.

Likewise, you can’t take textureArray.length even if you have the four Textures inside. Because if you get random number ‘4’ it will throw an error. because Texture1 = Index[0], Texture2 = Index[1], Texture3 = Index[2], Texture4 = Index[3]… there is always nothing for the last number of length when used as Index.
You need to take (textureArray.length-1).

But next thing I’d be slightly worried about… does Random.Range give an Int? Won’t it return a float? You might need to process the random Number further, before you can use it. I’m not sure though… maybe it’ll work like this…

// if you know you won't add any new elements in runtime/on the fly to the array, 
//instead of 
var textureArray : Texture2D[];
// use
var textureArray : Texture2D[] = new Texture2D[4];
// to (ever so slightly) improve performance.

I slightly edited the following. I think it should work if you c&p it to your script…

//drag and drop your textures in the editor to these vars
var tex1 :Texture2d;
var tex2 :Texture2d;
var tex3 :Texture2d;
var tex4 :Texture2d;

//add the elements to the array
function Awake ()
{
  textureArray[0] = tex1;
  textureArray[1] = tex2;
  textureArray[2] = tex3;
  textureArray[3] = tex4;
}

In your if(theCollision.gameObject.tag == "Player" && First Texture) you need to replace the ‘First Texture’ with renderer.material.mainTexture == textureArray[0] and so on.

When you are done with this, I think you shouldn’t get any errors (?) … try to let unity compile it and tell us how it went…

Greetz, Ky.