Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ian-McCleary · Apr 23, 2014 at 03:54 AM · javascriptgui

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;
     }        
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Apr 23, 2014 at 04:00 AM 0
Share

Little hard to follow but...what sets Crafting[2] == true? $$anonymous$$aybe you wanted to add that to your if(InputGet$$anonymous$$ey...)

avatar image Ian-McCleary · Apr 23, 2014 at 04:10 AM 0
Share

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.

avatar image getyour411 · Apr 23, 2014 at 04:15 AM 0
Share

If you aren't getting Debug.Log(Ammount) then Unity begs to differ

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ian-McCleary · Apr 24, 2014 at 01:45 AM 0
Share

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;
         }
     }
 }    
avatar image SeeSharp · Apr 24, 2014 at 02:12 AM 0
Share

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.

avatar image Ian-McCleary · Apr 25, 2014 at 12:19 AM 0
Share

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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

It is not possible to invoke an expression of type 'UnityEngine.GameObject'? 1 Answer

Is this a problem of unity 3 or i am doing something wrong. 1 Answer

What's wrong with my function? It keeps telling me 'loseText' is not a member of 'UnityEngine.GUIText'. Please help, please and thank you! 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges