- Home /
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;
}
}
After looking at the script some more I was wondering if the double if's were redundant on the else if statements (such as below)
else if{ if(theCollision.gameObject.tag == "Player" && HealTexture){ GiveHeal;
could or should this be changed or is the double if not a problem?
In an attempt to test the script to figure out where errors are I have broken the script in half to see if I could simply get the color's to switch using the top portion of the script (everything above the OnTriggerEnter function) and have realized that the textures aren't even switching so something in that section is broken. now to figure out what...
Taking another look at the timer section of the script I am wondering if I may have gotten it wrong... seems now that it is saying..
the timer is = to time elapsed +10 and if the time is greater then the timer to do the random number and switch textures and reset back to current time +10 being that the timer is going to be above itself 90% of the time (since it doesn't get set to a lower value) this seems ineffective.. What I was going for was something more along the lines of either having the timer start at 0 and when hitting the interval random number texture generate then reset to 0 or vice versa have timer at 10 count down to 0 then random switch and reset back to 10..
so I think I need to do something more like...
timer = Time.time;
if(timer >= interval){ random switching here
timer = 0 + Time.time;
or something along these lines?
Not sure if the Edit or the comment is more recent (my browser screws this up a little) ... if the eof ('end of file' expected) is still a current problem, you still have a your brackets screwed. Try to align them better still, to see what's actually lining with what. $$anonymous$$aybe you'll find this helpful? http://answers.unity3d.com/questions/155200/stuff-is-going-wacky-checklist-compiler-errors-syn.html#answer-155201 I'm too tired to check out your other problems/suggestions. I'll probably check back tomorrow. Greetz, $$anonymous$$y.
I'm a bit torn about your practice to replace the whole thing in your edits... =/
On the one hand, your script is quite long and adding the complete code each time you worked it over would make this HUG$$anonymous$$..
but on the other hand, unity answers is meant to be a knowledge-database. If you delete the previous versions, no one else reading this will ever get what people in the answers are talking about =/
I would appreciate, if you could at least have the 'original' and the 'newest' version. (To add in the original, you can hit the 'edit' button; then at the very top in the dropdown-menu 'revision' select the first revision and copy the text; then enter it in with some note of 'this is how it initiately looked' at top or bottom of the current version...) That would be great :)
Greetz, $$anonymous$$y.
Answer by SilverTabby · Sep 11, 2011 at 07:33 AM
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.
Thank you for the positive feedback SilverTabby. I know there are bracket errors I am trying to figure them out currently some of the missing ones you pointed out I removed cause Unity was throwing a fit about them. I probably need to comb through the script slowly re-doing all of them. I am using a script editor right now called Notepad2 extended. I have never used or messed with arrays much so that thought never even came to me I'm going to look up and try and educate myself on them a bit and try your idea of the array for the texture changes.. as far as the long list of functions for all the effects are they fine? Unity seems to be throwing a fit about me using else in the trigger functions, but that could be because of the previous syntax and bracket errors.
I have taken the time to completely go through the script and make it nice and pretty so the readability and functionallity of it has greatly improved. I do believe I have removed all syntax and bracket errors at this point. Only one thing left (I think) to get this script working, which I have addressed in the OP.
Answer by SisterKy · Sep 12, 2011 at 02:54 PM
About the questions in comments about 'if' and 'else if':
if(myTimer > 0) { myTimer -= Time.deltaTime; }
if(myTimer ]
Here, you could replace the 'if (myTimer
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.
Answer by SisterKy · Sep 12, 2011 at 03:27 PM
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.
Thanks a ton Sister$$anonymous$$y I think you got it working. Actually after I made the above changes I had about 700 errors pop up but they were studip errors on my part that I went through and fixed.. Now I only have 2 errors left I am hoping you can help me solve.. they are:
BCE0051: Operator '+' cannot be used with a left hand side of type 'int' and a right hand side of type 'System.Type'. (this pops up 4 times for where I try to increase ammo and points, but only when I use a var for the amount rather then an int)
and
BCE0019: 'Add' is not a member of 'UnityEngine.Texture2D[]'.
I have posted the new script with changes made above in case I messed something up somewhere.
ok I have once again updated the "updated" script above. This seems to have no errors and I think the method used to call the textures in the array is correct (read a couple other posts about texture arrays)I am about to test the script. However I would like to figure out still how to replace the int with a var for the give ammo and give points portions of the script.
Okay well so the above script does not have any errors from unity it also does not seem to be working... So still trying to figure that part out
happy I can be of help :)
oops... yes... the bce0019 was because I was thinking list-array ins$$anonymous$$d of built-in array. I suppose you did textureArray[1] = tex1;
?
I expect Damage and Heal gives that error as well, not just Ammo and Points? The problem is (or was?) your declaration. var damageAmount = int;
needs to be var damageAmount : int;
Since you say you don't get any errors any more, I suppose you already had fixed those yourself... so what exactly is not working? $$anonymous$$aybe you can try to add some print(); to get debug-output where the problem might be?
Greetz, $$anonymous$$y.
No I have not done textureArray[1] = tex1; cause I haven't figured it out yet I tried doing it in a start function but got error :BCE0022: Cannot convert 'System.Type' to 'UnityEngine.Texture2D'.
After adding a Debug.Log to each of the Trigger possibilities it seems to only be detecting the trigger collision for the GiveDamage, but it isn't applying the damage.