- Home /
How to connect two functions (Javascript)
I don't think i made the question very clear in the title so here is a little explaining.
I have a button in my function OnGUI and i would like for it to trigger a few lines of code in my function update (its for crafting) but im not 100% sure on how to connect the two of them.
I would like for the Crafting function to always be on but i only want the button to show when Tab is pressed. So far i have it when tab is pressed the inventory is on and everything works well except the button wont craft the objects because it has no way to say that the crafting has been in place.
I hope this made a little sense and i put a few of my variables and the lines of code that i am having the problems with. I paraphrased my script below so i cut out many of the irrelevant parts of it. For a little more information the Debug.Log("button"); does show up in the console but the Debug.Log("Ammount"); does not and yes i do know amount is spelled with one m.
Thank you in advance!
var Crafting : boolean[];
var Ammount : int[] = [0,0,0,0];
function OnGUI()
{
if(InventoryOn == true)
{
if(GUI.Button(Rect(200,500,80,30),"Craft Item"))
{
Debug.Log("button");
}
function Update ()
{
if(Crafting[2] == true)
{
Debug.Log("Ammount");
if(Ammount[0] >=1 && Ammount[1] >=1)
{
Ammount[0]-=1;
Ammount[1]-=1;
Ammount[2]+=1;
Crafting[2] = false;
}
}
if(Input.GetKeyDown(KeyCode.Tab))
{
InventoryOn = !InventoryOn;
}
Little hard to follow but...what sets Crafting[2] == true? $$anonymous$$aybe you wanted to add that to your if(InputGet$$anonymous$$ey...)
Crafting is the function for making items and the numbers inside the array are the item number that will be crafted. I have assigned number two to a object.
If you aren't getting Debug.Log(Ammount) then Unity begs to differ
Answer by SeeSharp · Apr 23, 2014 at 03:58 AM
Hi,
You could just make a function that has all the code you currently have in your update, and then just call it on update/whenever you need.
If I didn't fully answer your question, or you need further assistance, please don't be afraid to ask.
Best of luck,
SeeSharp.
So i rearanged some things in the script but it still is not working. This time i get all the Debug.Logs except for "Ammount1" so i think it might be an error with the item names but if not here are the changes i made.
function OnGUI ()
if(GUI.Button(Rect(200,500,80,30),"Craft Item"))
{
gameObject.Send$$anonymous$$essage("Craft2");
Debug.Log("button");
}
function Craft2 ()
{
if(Crafting[2] == true)
{
Debug.Log("Ammount");
if(Ammount[0] >=1 && Ammount[1] >=1)
{
Ammount[0]-=1;
Ammount[1]-=1;
Ammount[2]+=1;
Debug.Log("Ammount1");
Crafting[2] = false;
}
}
}
Hi,
The only explanation to that is that one of your variables isn't set as you expect it to be. $$anonymous$$aybe try to set all your variables to what you need them to be, or comment out the if statements and make sure it works without them. If it does actually work, go to where the values are set and make sure that they are being called. You could also use Debug.Log to display the values.
Best of luck,
SeeSharp.
It turns out that my var ItemNumber was given a set amount such 1 so everything i collected went to only Ammount[1]
Thanks for the help!