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 Zmiho · Jan 15, 2014 at 05:17 PM · textureobjectinventorycheckindicator

How to check if player has an object in inventory

So...after the player picks up an object(flashlight i.e.), it goes to the inventory. How to check if the player has flashlight in inventory so he can use it. If the player doesn't pick up flashlight, he can't use it.

And after that, how to add small texture (indicator) of flashlight that shows that you have picked up the flashlight.

Flashlight script (js) #pragma strict

 var LightIntesity = 2.0;
 var LightRange = 16.0;
 var LightSpotAngle = 42.0;
 
 var AudioFile1 : AudioClip;
 var AudioFile2 : AudioClip;
 
 bool CheckForItem (Item "Flaslight") {
     if (inventory.Contains("Flashlight")) {return true;}
     else {return false;}
 }
 
 function Start (){
     light.intensity = LightIntesity;
     light.range = LightRange;
     light.spotAngle = LightSpotAngle;
 }
 
 function Update (){
     if (Input.GetKeyDown(KeyCode.F)){        
         if (light.enabled == false){
         light.enabled = true;
         audio.clip = AudioFile1;
         audio.Play();
         }
         else{
         light.enabled = false;
         audio.clip = AudioFile2;
         audio.Play();
         }
     }
 }



Pickup script (js) #pragma strict

 public var keyGrab : AudioClip;                         // Audioclip to play when the key is picked up.
 
 
 private var player : GameObject;                        // Reference to the player.
 private var playerInventory : PlayerInventory;      // Reference to the player's inventory.
 
 
 function Awake ()
 {
     // Setting up the references.
     player = GameObject.FindGameObjectWithTag("Player");
     playerInventory = player.GetComponent(PlayerInventory);
 }
 
 
 function OnTriggerEnter (other : Collider)
 {
     // If the colliding gameobject is the player...
     if(other.gameObject == player)
     {
         // ... play the clip at the position of the key...
         AudioSource.PlayClipAtPoint(keyGrab, transform.position);
         
         // ... the player has a key ...
         playerInventory.hasKey = true;
         
         // ... and destroy this gameobject.
         Destroy(gameObject);
     }
 }



PlayerInventory (js) #pragma strict

 public var hasKey : boolean;
Comment
Add comment · Show 4
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 roojerry · Jan 15, 2014 at 06:52 PM 0
Share

It would probably help to see the PlayerInventory class. In there should be defined whether the user has a flashlight or not

avatar image Zmiho · Jan 15, 2014 at 06:59 PM 0
Share

I've addded it now

avatar image roojerry · Jan 15, 2014 at 07:04 PM 0
Share

Well... you have a has$$anonymous$$ey variable. How about adding a hasFlashlight variable to your inventory.

avatar image Zmiho · Jan 15, 2014 at 07:38 PM 0
Share

that still isn't the right answer...haha btw I did it on purpose

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kei_kvaartz · Jan 15, 2014 at 06:32 PM

Without seeing any code, I would assume the inventory is kept as a List. In that case I would use something like:

 bool CheckForItem (Item wantedItem) {
     if (inventory.Contains(wantedItem)) {return true;}
     else {return false;}
 }
Comment
Add comment · Show 1 · 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 Zmiho · Jan 15, 2014 at 06:47 PM 0
Share

it doesn't work

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

20 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

Related Questions

Picking up an object 2 Answers

inventory system - change GUI.DrawTexture texture 3 Answers

Separate textures for identical objects? 1 Answer

How would I implement this game mechanic? 0 Answers

create a menu to change texture on a certain object 1 Answer


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