I am currently working on a tower defence game as some already know, and i got a script wich places towers by click and now i’m adding a money system so that the towers cost money, now the problem is that since i’m not a scripter that i can’t get this part really done. Soo my question is that in the following script, where i have to put(and what code) to subtract towerPrice from totalMoney,
var TowerPrefab : Transform;
var totalMoney = 3000;
var towerPrice = 900;
if(towerPrice < totalMoney);
function Update () {
if(Input.GetButtonDown("Fire1"))
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
Instantiate(TowerPrefab, hit.point, Quaternion.identity);
}
}
else();
uhm i tried the code now but what is supposed to happen is that you can’t place any towers when you don’t have enough money, i thought this would work but it’s not working, maybe anybody knows how to fix that?
Well you need to check if you have enough money, don’t you?
So you’ll need another if(totalMoney equals or are more than “enough”) THEN instantiate it.
And I agree with FizixMan (lol nice name!) take a look at the links he posted
The if statements should be inside the Update() function and you need to check it before instantiating. So
stands for AND. if both the raycasting AND the towerPice statements are true, then it will instantiate. If only one of them or none of them are true, then it won’t instantiate.
Yes I was wondering why you didn’t get that earlier, since the syntax is wrong. I suggest you start small, learn the syntax, write a hello world, then write a conversation program, where the computer asks for your name, and then says “Hello, [name]!” and if your age is more than 12 and less than 20, it will reply “a teenager!” else if it’s 20 or more, it will say “An adult!”, else it just says “A youngster/kid/person!” or something.
This may seem boring but it’s extremely helpful for learning the syntax and to think correctly, I’ve done it plenty of times in all programing languages I’ve learnt. And it will be fun once you write fun sentences, like once my program said “An adult! How you doin’? blink” when I tested to write if-statements (I’m easily amused haha)
Well the reason that i’m already scripting a game is because it’s for a school project, and i am the only one that has to make the script instead of getting a script because they believe i can do that, but i really need the script or else i’ll get an F xD so that’s why i ask so much.
and another question right away xD what i noticed was that even when the tower stops being placed, when i keep clicking my totalMoney still counts down, but that has to stop to, any suggestions on how to do that xD i have been trying out some stuff but that’s not really working xD
Oh ok poor you
Well yes, when writing any kind of statement, it will only be applied for the row just below.
So you need to gather all the stuff inside the if-block, by placing them between brackets { }
aah okay i’ll look at that then, uhm only one last question i know that i can give objects different layers, now is it possible to say like
if(layer == Layer1)
Instantiate(TowerPrefab, hit.point, Quaternion)