Money system help!

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();
if (Physics.Raycast (ray, hit)) 
{
	Instantiate(TowerPrefab, hit.point, Quaternion.identity); 
	totalMoney = totalMoney - towerPrice;
}

I also suggest you read tonyd’s excellent guide to learn scripting: http://forum.unity3d.com/threads/34015-Newbie-guide-to-Unity-Javascript-(long)
And also try going over some of Unity’s tutorials and resources to learn from. :smile:

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? :slight_smile:
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 :wink:

i now have this:

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); 
				totalMoney = totalMoney - towerPrice;
		}
	}
else	(towerPrice => totalMoney)	
					Debug.Log("no money");

and i do get “no money” in the debug log but how can i make the tower placing stop?

Ow and i got this error code because of the else statement: Assets/SCripts/turretplacing.js(17,1): BCE0044: expecting EOF, found ‘else’.

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.

/Kweiko

Yeah it finally worked Thanks a lot
now one thing left to do, making the countdown stop when i click again xD i’ll try doing that myself for once:P

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 :stuck_out_tongue: (I’m easily amused haha)

/Kweiko

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

never mind already fixed :slight_smile:

Oh ok poor you :stuck_out_tongue:
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 { }

Wrong:

RIGHT:

I now got this as code ( wich works exactly like i want)

only problem is i think some codes can be shorter but i don’t know how, though i don’t really care but it just looks alot cleaner :slight_smile:

var TowerPrefab : Transform;
var totalMoney = 3000;
var towerPrice = 900;
	
	function Update () {
		if(Input.GetButtonDown("Fire1"))
		{
			var ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
			var hit : RaycastHit; 
			if(Physics.Raycast(ray, hit)  towerPrice <= totalMoney)
				Instantiate(TowerPrefab, hit.point, Quaternion.identity); 
			if(Physics.Raycast(ray, hit)  towerPrice <= totalMoney)
				totalMoney = totalMoney - towerPrice;
		}
	}

yes it can be a bit shorter, see my previews post! :slight_smile:

/Kweiko

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)

Something like that for example

yeah it’s possible, but it will trouble you more to try and optimize the code, if things aren’t broken, then why fix them? :stuck_out_tongue: atleast at this stage.

but yeah something like that should work, I don’t have access to unity right now. But you need to test your way to the right syntax
/Kweiko

Okay thanks alot :slight_smile:

I had that exact learning exercise in a very old How to write BBC Basic book … aaah the memories :slight_smile:

haha yeah I loved those times. Back then it took me 3 days to write it without errors X)