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 RespawnGames · Nov 02, 2014 at 11:30 PM · javascriptinventory

Item pickup add to inventory?

I've spent the entire day trying to figure this out to no avail. I have created a script for a simple inventory and I am trying to make the item I want the player to be able to pick up add to the inventory. Its a really simple text inventory but it doesn't seem to work. Here's the code for the inventory:

 #pragma strict
 
 var showGUI : boolean = false;
 var woodInv : int;
 
 woodInv = woodPickup.woodInvAmount;
 
 function Update () 
 {
     if (Input.GetKeyDown(KeyCode.F))
         {
         if(showGUI == false)
         {
             showGUI = true;
             Debug.Log("showGUI = true");
         } else{
             showGUI = false;
             Debug.Log("showGUI = false");
         }
     }
 }
 
 function OnGUI()
 {
     if(showGUI == true)
     {
         GUI.Box (Rect (10,10,100,90), "Inventory");
         GUI.Label (Rect (15,35,40,30), "Wood:");
         GUI.Label (Rect (75,35,40,30), " " + woodInv);
     }
 }

And here is the code for the wood pickup item:

 #pragma strict
 static var woodInvAmount : int = 0;
 
 function OnTriggerEnter (Col : Collider) 
 {
     if(Col.tag == "Player")
     {
         ++woodInvAmount;
         Destroy(gameObject);
     }
 }

Any Help would be very much appreciated!

*P.S. This is in UnityScript

Comment
Add comment · Show 2
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 richyrich · Nov 03, 2014 at 12:07 AM 1
Share

Try putting this

 woodInv = woodPickup.woodInvAmount;

into function Update...

 function Update ()
 {
     woodInv = woodPickup.woodInvAmount;
     if(...
avatar image richyrich · Nov 03, 2014 at 12:11 AM 0
Share

As it is the player that moves into objects, rather than the other way around, why is it that the trigger collision is not made inside a player script? This would negate the need for what currently appears to be a duplication of data

2 Replies

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

Answer by Serinx · Nov 03, 2014 at 12:31 AM

It looks like your woodInvAmount is being set initially but never updated so only the initial value of 0 will be used.

A quick fix would be to move the line "woodInv = woodPickup.woodInvAmount;" into the update function.

If it were me I would add a function to the Inventory script called "WoodPickup" or something and pass the woodInvAmount as a parameter, the passed in value could then be added to the private WoodInv integer and displayed.

You could then reference this script from within your Wood Pickup Item script and call the function before destroying the object.

Good Luck!

Comment
Add comment · 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
1

Answer by Kiwasi · Nov 03, 2014 at 12:20 AM

@richyrich has the problem right. Change line 29 to:

 GUI.Label (Rect (75,35,40,30), " " + woodPickup.woodInvAmount);

Note that you are heading down a dangerous path using static variables this way. Some deep thinking about your desired structure is probably warranted.

Comment
Add comment · Show 5 · 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 RespawnGames · Nov 03, 2014 at 01:18 AM 1
Share

Thanks a lot everyone for the help! $$anonymous$$oving the woodInv variable into update fixed it! I'm a it of a novice when it comes to program$$anonymous$$g, I'm more of an art guy, so I was surprised to even have gotten that far. Now, Bored$$anonymous$$ormon, do you think you could elaborate on what you mean by heading down a dangerous path? Like I said before I kind of new to program$$anonymous$$g so I was just trying to figure out how to get it working in anyway. Thanks!

avatar image Kiwasi · Nov 03, 2014 at 01:42 AM 0
Share

Static variables belong to the class. So you can only ever have one instance of them. Your inventory system will fall down as soon as you decide to have more then one player.

You also want to consider ownership of the variable. Does it really make sense for your wood pickup to be script to be controlling your player inventory? Each class should have one, and only one responsibility. (That principle gets stretched a bit, but the idea is sound).

Here is an alternate structure. Its C#, because that's the better language :). But the principle is the same.

 public class Inventory : $$anonymous$$onoBehaviour {
     public int woodInv;
 
     // Your display code as before
 }
 
 public class WoodPickUp : $$anonymous$$onoBehaviour {
     void OnTriggerEnter (Collider other) {
         Inventory inventory = other.gameObject.GetComponent<Inventory>();
         if(inventory){
             inventory.woodInv++;
             Destroy(gameObject);
         }
     }
 }

The advantage of this structure becomes apparent really quickly if you add a second agent, or if you add another script that needs to add or subtract wood from the inventory. It makes far more sense for your shop to access the players inventory then it does to access the WoodPickUp script.

Edit: Worth noting that GetComponent is more expensive then tag checking. Tag checking makes sense if every GameObject with inventory is tagged the same. Send$$anonymous$$essage can also be used for interscript communication.

avatar image RespawnGames · Nov 03, 2014 at 01:49 AM 0
Share

I see. I never thought about that, thank you. So it would be better to then create a new script that basically controls all of the inventory related actions, and use classes for everything than to have a separate scripts for it all? Sorry for all the questions.

avatar image Kiwasi · Nov 03, 2014 at 01:56 AM 0
Share

The two classes are both meant to be different scripts. That's one of the differences between C# and JavaScript. In JavaScript the class declaration is implicit. In general the rule is each class/script should do only one job.

You want one inventory script that controls all of the inventory levels. Depending on how complex it gets you could also have that script do the GUI stuff.

You want a separate script on your pick up items.

avatar image RespawnGames · Nov 03, 2014 at 01:59 AM 0
Share

Oh okay, I think I understand now. Again, sorry for all the questions. Thanks a lot!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Setting Scroll View Width GUILayout 1 Answer

How to simplify my Equipment method? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Script Not Working 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