Some help with variables ?

hello could somebody tell me how to make a variable that stop in a certain number.
Like in a shop where you can’t buy the item if you don’t have more than 10 coins.

And I had been very happy if somebody also could tell me how to make a game save if you hit a button or something.

THANKS…

Hrmmm… Well this is being made from scratch, so be prepared for syntax errors lol:

var minMoney = 10;
var money = 9;


function gainMoney ()
{
money ++;
}

function buy ()
{
var itemName = "potion"
if (money < minMoney)
{
print("You don't have enough money!")// I never use print, and probably got the syntax wrong lol
}
else
{
money -= minMoney;
print("You received the " + itemName + ", and lost " + minMoney + "moneys!")
}
}

ey, thanks man…
going to test it right now

Well for me, print does not do anything lol, best to use Debug.log instead.

The code worked just fine,but one thing that could be fine is to don’t use the print. Instead get the text on the screen and not in the debug panel.

The code I used was

var minMoney = 10; 
var money = 20; 
var itemName = "ITEM";



function OnMouseDown () { 
	if (money < minMoney) {
		print("not enough moneys");
	}
	else
	{
	money -=minMoney;
	print("you got the" + itemName + ",and lost" + minMoney + "moneys");
	}

}

The code was used on a GUI text to push on …

You can use the OnGUI function in Unity, let me see here…

EDIT: I am almost done here, one second…

yea I’m looking around and almost got it…
just one thing that I’ve forgot is get a variable from another script … :smile:

Yeah, I can get that in too lol, let me finish THIS script first though :lol:

Ok, done, I added some more customizability hehehe…

var minMoney = 10; 
var money = 20; 
var itemName = "ITEM"; 
var currency = "dollar";
var items = 0;
var message = "";



function OnGUI () 
{ 
GUI.Label (Rect (10, 10, 500, 20), "You have " + money + " " + currency + "(s)");
GUI.Label (Rect (10, 30, 500, 20), "You can buy the " + itemName + " for " + minMoney + " " + currency +"(s)" );   

GUI.Label (Rect (10, 50, 500, 20), "You have " + items + " " + itemName + "(s)!"); 

GUI.Label (Rect (10, 70, 500, 20), message);

if (GUI.Button (Rect (10, 90, 100, 20), "Get money!")) 
{
money ++;
}

if (GUI.Button (Rect (110, 90, 100, 20), "Buy!")) 
{
if (money < minMoney) 
    { 
    message = "not enough " + currency + "s!";
    } 
    else 
    { 
    money -=minMoney; 
    message = "you got the " + itemName + ", and lost " + minMoney + " " + currency + "s!";
    items++;
    } 
}
   

}

ok, THANKS
I would test it out and see if I understand it…
:lol:

If you need help understanding any part of it, just ask me, and I can explain to you exactly what that/those part(s) do, and (possibly) explain it line for line.

OK, thanks again.
I do think I have understand it right.

  1. I put this on a Empty game object
  2. It crates a lot of GUI elements Who changes by the variables.
  3. I just look sooooo complicated but it isn’t …

hehe :smile:

AND just one more thing if you have time…?
How to change the font of the text.

The code worked just fine and I understand it :smile:

lol yup, OnGUI can get like that. Here is my resource for whenever I need to GUI help!

Gui Elements