• 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
Question by DubstepDragon · Feb 23, 2013 at 10:40 PM · triggercenteronmouseenter

Error with picking up an item...

I have a "for" loop that should give the user an item. I have the inventory scripts set up already, everything in good order. However, I do not know how to add the item to the inventory when the player presses "E" and the mouse cursor is over the item (cursor locked in screen center and cross-hair set up).

     for(var x = 0; x < loot.length; x++) {
         Inventory.playerInventory.Add(loot[x]);
     }


Can anyone help me? Do I use OnMouseEnter?

Thanks in advance!

Comment

People who like this

0 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 theundergod · Feb 23, 2013 at 10:51 PM 1
Share

Are you trying to accomplish this in a GUI or in 3D space?

avatar image DubstepDragon · Feb 24, 2013 at 08:57 AM 0
Share

The picking up of an item is in 3D space, with a script to destroy the object after it is used. But I cannot use OnMouseOver, because this looting method is already a function itself, and cannot be used within another function. Here is the code:

 #pragma strict
 
 var loot : ItemClass[];
 var Inventory : Inventory;
 
 function Start () {
 
 }
 
 function Update () {
 
 }
 
 function Giveloot() {
     
     for(var x = 0; x < loot.length; x++) {
         Inventory.playerInventory.Add(loot[x]);
     }
     
 }


And my Inventory Script:

 #pragma strict
 
 import System.Collections.Generic;
 
 var playerInventory : List.<ItemClass> = new List.<ItemClass>();
 var scrollView : Vector2;
 
 var equippedItem : ItemClass;
 var equipped : boolean = false;
 
 var playerHand : Transform;
 
 function Update () {
     if(equippedItem.itemType == ItemClass.ItemType.Weapon) {
         equipped = true;
     }
     else {
         equipped = false;
     }
 }
 
 function OnGUI() {
     //INVENTORY
     GUILayout.BeginArea(Rect(Screen.width - 500, 0, 500, 500));
     
     scrollView = GUILayout.BeginScrollView(scrollView, GUILayout.Width(500), GUILayout.Height(500));
     
     for(var x = 0; x < playerInventory.Count; x++) {
     
         GUILayout.BeginHorizontal();
         
         if(GUILayout.Button(playerInventory[x].icon) && playerInventory[x].itemType == ItemClass.ItemType.Weapon && equipped == false) {
         
             equippedItem = playerInventory[x];
             var weapon : Transform = Instantiate(playerInventory[x].itemPrefab, playerHand.position, Quaternion.identity);
             weapon.parent = playerHand;
             playerInventory.RemoveAt(x);
             return;
             
         }
         
         GUILayout.Box(playerInventory[x].name);
         GUILayout.EndHorizontal();
         
         GUILayout.Box(playerInventory[x].description);
         
     }
     
     GUILayout.EndScrollView();
     GUILayout.EndArea();
     
     
     //WEAPON SLOT
     
     if(GUI.Button(Rect(Screen.width / 2 - 25, Screen.height - 50, 50, 50), equippedItem.icon) && equipped == true) {
     
         playerInventory.Add(equippedItem);
         Destroy(GameObject.FindGameObjectWithTag("Weapon"));
         equippedItem = new ItemClass();
         
     }
 }



Hope this comes in any use to you guys.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by MikeNewall · Feb 23, 2013 at 10:54 PM

OnMouseOver() Is triggered when you mouseover an objects collider or a gui element.

   function OnMouseOver() 
     {
         if(Input.GetKeyDown("e"))
         {
            //Do your inventory stuff here
         }
     }
Comment
theundergod
DubstepDragon

People who like this

2 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

A node in a childnode? 1 Answer

Trigger door animation with external trigger 1 Answer

Unity Script Refernece 1 Answer

Trigger enter issue 1 Answer

Problems with instantiation... 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