Okay so this is my first real attempt at writing a script with any type of complexity to it so I apologize in advance if it makes anyone’s eyes bleed. I have attempted to write this purely through what I have learned the last few weeks dealing with script and have minimized my google searching for answers to prevent having a script handed to me. So I ask that any answers not be complete scripts but instead just stear me in the right direction as I am using this as a learning process.
The script below I am attempting to do the following with. I want it to go on a tile on the ground that will (on a timer) change between 4 textures. With each texture I want there to be a different effect given to the player if he is on, or enters, the trigger. the 4 effects are to hurt him, heal him, give him points and give him ammo.
Any input anyone has for this oh so crude attempt at scripting is much appreciated. I am specifically looking for any information pertaining to cleaning the script up and making it more efficient (is there a better way to have written any of it?), any smack in the face errors that stick out, and why unity is giving me an EoF error on the else portions of the trigger functions (are if/else not allowed in trigger functions?). I have taken the time to add a lot of comments to try and give as much information as to what I am trying to do with each part of the script.
var healTexture : Texture2d;
var hurtTexture : Texture2d;
var ammoTexture : Texture2d;
var pointTexture : 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 timer:float;
private var nextDamageAllowed : float;
private var nextHealAllowed : float;
private var nextAmmoAllowed : float;
private var nextPointAllowed : float;
function Start(){
timer = Time.time + interval; //the timer is equal to the time in the future
}
function Update(){
if(Time.time >= timer) //if the current time elapsed is equal to or greater than the timer
randomNumer = random.range (1,4);
}
switch(randomNumber){
case 1: HealTexture;//If number is 1, apply HealTexture Function
case 2: HurtTexture;//If number is 2, apply HurtTexture Function
case 3: AmmoTexture;//If number is 1, apply AmmoTexture Function
case 4: PointTexture;//If number is 2, apply PointTexture Function
}
timer = Time.time + interval; //reset the timer
//Apply the healTexture to object
function HealTexture(){
renderer.material.mainTexture = healTexture;
}
//Apply the hurtTexture to object
function HurtTexture(){
renderer.material.mainTexture = hurtTexture;
}
//Apply the ammoTexture to object
function AmmoTexture(){
renderer.material.mainTexture = ammoTexture;
}
//Apply the pointTexture to object
function PointTexture(){
renderer.material.mainTexture = pointsTexture;
}
//When player enters trigger hurt them if the hurtTexture is active
function OnTriggerEnter(thecollision : Collider){
if(theCollision.gameObject.tag == “Player” HurtTexture){
GiveDamage;
}
}
//when player enters trigger heal them if the healTexture is active
else if{
if(theCollision.gameObject.tag == “Player” HealTexture){
GiveHeal;
}
}
//when player enters trigger give them ammo if the AmmoTexture is active
else if{
if(theCollision.gameObject.tag == “Player” AmmoTexture){
GiveAmmo;
}
}
//when player enters trigger give them points if the PointTexture is active
else{
if(theCollision.gameObject.tag == “Player” PointTexture){
GivePoints;
}
//when player stands on trigger hurt them periodically if the HurtTexture is active
function OnTriggerStay(thecollision : Collider){
if(theCollision.gameObject.tag == “Player” HurtTexture){
GivePeriodicDamage;
}
}
//when player stands on trigger heal them periodically if the healTexture is active
else if{
if(theCollision.gameObject.tag == “Player” HealTexture){
GivePeriodicHeal;
}
}
//when player stands on trigger give them ammo periodically if the AmmoTexture is active
else if{
if(theCollision.gameObject.tag == “Player” AmmoTexture){
GivePeriodicAmmo;
}
}
//when player stands on trigger give them points periodically if the PointTexture is active
}else{
if(theCollision.gameObject.tag == “Player” PointTexture){
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;
}
}