Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 seas726 · Sep 21, 2016 at 01:31 PM · javascripttransforminventoryitem

Item Icon goes to origin, not set transform. (JAVASCRIPT)

Hi all. I am making an inventory system for my fps game, and I have come across an issue. I have a bunch of panels that are activated and deactivated in order to show/hide my inventory. In the start function, I populate a panel with slots for an item. Then, in game, I can pick up items, and an item icon should be instantiated as a child of one of my available slots, and set in the center, (0,0). This works fine if I don't open my inventory (e) before picking up my item. The problem is when I open my inventory, close it, and try to pick it up. Then it goes to the origin, and is visible in the lower left corner. I will attach both scripts I am using in this process. Any help would be greatly appreciated, thanks!

Inventory Script:

 var itemsScreen : GameObject;
 var inventorySlot : GameObject;
 var inventoryItem : GameObject;
 var inventoryPanel : GameObject;
 var slotPanel : GameObject;
 var player : GameObject;
 var ammoText : GameObject;
 var crosshair : GameObject;
 /////////////////////////////////////////////
 var selectedSlot : Transform;
 var selectedItem : Transform;
 var originalSlot : Transform;
 /////////////////////////////////////////////
 var isPause : boolean;
 var inventoryUp : boolean;
 var isCreated : boolean;
 var receivedItem : boolean;
 var debuged : boolean;
 ////////////////////////////////////////////
 var slotAmount : int;
 var itemAmount : int;
 var itemId : int;
 /////////////////////////////////////////////
 var items : Item[];
 var equipMenu : Item[];
 var mainInventory : List.<Item> = new List.<Item>();
 var slots : List.<GameObject> = new List.<GameObject>();
 ////////////////////////////////////////////End variables
 function Start () {
 
 slotAmount = 16;
 //inventoryPanel = GameObject.Find("ItemPanel");
 //slotPanel = inventoryPanel.transform.FindChild("SlotPanel").gameObject;
 
 inventoryScreen.SetActive(false);
 inventoryUp = false;
 
 
 for(var q = 0; q < slotAmount; q++){
 
         slots.Add(Instantiate(inventorySlot));
         slots[q].transform.SetParent(slotPanel.transform);
         slots[q].GetComponent.<slotScript>().manager = gameObject;
         }
 
     ///////////////////////////////////////////////
 }
 
 
 function Update () {
 
 receivedItem = player.GetComponent.<ItemPickUp>().receivedItem;
 itemId = player.GetComponent.<ItemPickUp>().itemID;
 itemAmount = mainInventory.Count;
 isPause = GetComponent.<PauseMnue>().isPause;
 //////////////////////////////////////////////////////
 
 
 if(Input.GetKeyDown("e") && isPause == false){
 
     Screen.lockCursor = false;
     inventoryScreen.SetActive(true);
     ammoText.SetActive(false);
     crosshair.SetActive(false);
     inventoryUp = true;
     Time.timeScale = 0;
     }
 
 ///////////////////////////////////////////////////////
 
 if(receivedItem){
 
 AddToInventory ();
 
 }
 
 ////////////////////////////////////////////////////////
 }
 function BackInventory () {
 
     inventoryScreen.SetActive(false);
     Time.timeScale = 1;
     inventoryUp = false;
     ammoText.SetActive(true);
 }
 
 ////////////////////////////////////////////////////////
 function BackItemsPanel () {
 
 itemsScreen.SetActive(false);
 
 }
 ///////////////////////////////////////////////////////////
 
 function AddToInventory () {
 
 for(var i = 0; i < itemAmount; i++){
     
     for(var qq = 0; qq < slotAmount; qq++){
 
 
     if(slots[qq].transform.childCount <= 1){
 
         var newItemSlot : GameObject = Instantiate(inventoryItem);
 
         isCreated = true;
         receivedItem = false;
 
         newItemSlot.transform.SetParent(slots[i].transform);
         newItemSlot.transform.position = Vector2(0,0);
     
         
 
         }
         else
         {
         if(!debuged){
         Debug.Log("Inventory full");
         debuged = true;
         }
         }
         }
         }
         }
 
 
 function OnGUI () {
 
 
 
 }


Item Pick Up Script:

 var manager : GameObject;
 var hand : GameObject;
 var oldWeapon : GameObject;
 var itemToPickUp : GameObject;
 var originalScale : Vector3;
 var canPickUp : boolean;
 public var isEquipped : boolean;
 public var receivedItem : boolean;
 var itemIIcon : Sprite;
 var itemID : int;
 var items : Item[];
 public var itemAmount : float;
 
 function Update () {
 ////////////////////////////////////////////////////////////////
 var mainInventory = manager.GetComponent(inventory).mainInventory;
 var items = manager.GetComponent(inventory).items;
 
 
     if(itemToPickUp){
 
     var test = itemToPickUp.GetComponent.<itemId>().itemId;
 
         }
         else
         {
 
             test = -1;
 
             itemAmount = items.Length;
 }
 
 
 for(var a = 0; a < itemAmount; a++){
 
     if(itemToPickUp){
         if(test == items[a].itemId){
             
             itemID = items[a].itemId;
 
             }
         }
 }
 
     if(canPickUp && Input.GetKeyDown("f")){
 
         receivedItem = true;
 
         mainInventory.Add(items[itemID]);
 
         Destroy (itemToPickUp);
 
         canPickup = false;
 
         }
         else
         {
 
         receivedItem = false;
 
 }
 if(hand.transform.childCount == 1){
 
     isEquipped = true;
 
     }
 }
 function OnTriggerEnter (other : Collider){
 
     if(other.gameObject.tag == "Weapon"){
         for(var z = 0; z < itemAmount; z++){
         canPickUp = true;
 
         itemToPickUp = other.gameObject;
         itemIIcon = other.gameObject.GetComponent.<itemId>().itemIcon;
         }
         }
     }
 
 function OnTriggerExit (other : Collider){
 
     if(other.gameObject.tag == "Weapon"){
 
         canPickUp = false;
 
         itemToPickUp = null;
     
         }
     }
 
 function OnGUI (){
 
 if(canPickUp){
     if(itemToPickUp){
     GUI.Button(Rect(Screen.width/2 - 50, Screen.height/2 + 10 , 125, 25),"Pick Up: " + itemToPickUp.name);
     }
     }
 }
 
 
 
 
 


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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Real Simple Inventory and Vendor System Help 1 Answer

Simplest movement to point possible 2 Answers

How to rotate instantated prefab without rotating it's local directions by javascript? 1 Answer

About Classes in js 2 Answers

Inventory Help. 2 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