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 Jamoy1993 · Jan 24, 2014 at 03:31 AM · errorarrayschildaccess

Need help accessing children of an parent from a remote script.

Hello i am currently trying to get my inventory game object where items are stored when they are in the inventory. I am trying to find out what objects have the property "ItemName" equal to my weapons AmmoType. in this case its 762x39...

However i can Debug.Log the ItemScript.ItemName and it comes out as expected which is 762x39

however for the ItemScript.Stack variable (of int type) its a different story it just returns null. instead of 50...

alt text

Contents is an array attached to the inventory script in my inventory game object.

 function Update()
 {
             if(Input.GetKeyDown(KeyCode.G))
             {
                 InventoryScript = Inventory.GetComponent("Inventory");
                 var invlen : int = InventoryScript.Contents.Length;
                 var totalammo : int = 0;
                 
                 
                 for(var x : int = 0; x < invlen; x ++)
                 {    
                 
                     var ItemScript : Item = InventoryScript.Contents[x].GetComponent("Item");
                                     
                     var curItemName : String = ItemScript.transform.ItemName;
                     
                     Debug.Log(InventoryScript.Contents[x].GetComponent("Item").Stack); //why the hell cant this return a int if it can return an string??????
                     if(curItemName == AmmoType)
                     {
                         var curStack : int = ItemScript.transform.Stack;
                         totalammo += curStack;
                     }
                                         
                 }
                 
             }
 }

can someone point out what im doing wrong?

wtf.jpg (317.5 kB)
Comment
Add comment · Show 1
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 neonblitzer · Jan 24, 2014 at 04:01 AM 0
Share

It would help if you posted the Inventory and Item scripts as well. Besides, calling ItemScript.transform.ItemName can't possibly work? Transform class has no such property.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jamoy1993 · Jan 24, 2014 at 04:59 AM

 **strong text**Item.js:
 
 var ItemName : String;
     var ItemDisplayName : String;
     var itemIcon : Texture2D; //The Icon.
     var canGet = true; //If we can pick up the Item.
     var itemType : String; //This will let us equip the item to specific slots. Ex: Head, Shoulder, or whatever we set up. If the item is equipment (or weapon) this needs to match a slot to work properly.
     var stackable = false; //Is it stackable? If yes then items with the same itemType will be stacked.
     var maxStack = 20; //How many Items each stack can have before creating a new one. Remember that the Items that should be stacked should have the same itemType.
     var stack = 1; //This is how many stack counts this Item will take up.
     var isEquipment = true; //Can the Item be equipped? This includes weapons.
     var isAlsoWeapon = false; //Is the Item also a Weapon? This only works with isEquipment set to true.
     //@HideInInspector
     var DropPos : GameObject;
     //This is the object we will instantiate in the Players hand.
     //We use this so we can have two versions of the weapon. One for picking up and one for using.
     var equippedWeaponVersion : Transform;
     
     //These will store information about usefull components.
     static var playersinv : Inventory;
     
     private var FPPickUpFound = false;
     
     @script AddComponentMenu ("Inventory/Items/Item")
     
     //Here we find the components we need.
     function Awake ()
     {
     
         DropPos = GameObject.FindWithTag("PlayerDropPos");
     
         playersinv = FindObjectOfType(Inventory); //finding the players inv.
         if (playersinv == null)
         {
             canGet = false;
             Debug.LogWarning("No 'Inventory' found in game. The Item " + transform.name + " has been disabled for pickup (canGet = false).");
         }
         else
         {
             gameObject.SendMessage("RetrievePlayer", playersinv, SendMessageOptions.DontRequireReceiver);
         }
         
         if (isEquipment == false && GetComponent(ItemEffect) == null)
         {
             Debug.LogError(gameObject.name + " is not equipment so please assign an ItemEffect script to it");
         }
         
         if (GetComponent(FirstPersonPickUp) != null)
         {
             FPPickUpFound = true;
         }
         else if (transform.GetComponentInChildren(FirstPersonPickUp) != null)
         {
             FPPickUpFound = true;
         }
     }
     
     //When you click an item
     function OnMouseDown()
     {
         //If the 'FirstPersonPickUp' script is not attached we want to pick up the item.
         if (FPPickUpFound == false)
         {
             PickUpItem();
         }
     }
     
     //Picking up the Item.
     function PickUpItem ()
     {
         var getit=true;
         if(canGet){//if its getable or hasnt been gotten.
         
         playersinv.gameObject.SendMessage ("PlayPickUpSound", SendMessageOptions.DontRequireReceiver); //Play sound
         
             if(stackable)
             {
                 var locatedit:Item;
                 var t : Transform;
                 
                 for(t in playersinv.Contents)
                 {
                     if(t.name==this.transform.name){//if the item we wanna stack this on has the same name
                         var i:Item=t.GetComponent(Item);
                         if(i.stack<i.maxStack)
                         {
                             locatedit=i;
                         }
                     }
                 }
                 if(locatedit!=null){//if we have a stack to stack it to!
                     getit=false;
                     
                     var spaceLeft : int;
                     spaceLeft = locatedit.maxStack - locatedit.stack;
     
                     if(GetComponent(Item).stack <= spaceLeft)
                     {
                         locatedit.stack += GetComponent(Item).stack;
                         Destroy(this.gameObject);
                     }
                     else
                     {
                         GetComponent(Item).stack -= spaceLeft;
                         locatedit.stack += spaceLeft;                            
                     }    
                 }
                 else{
                     getit=true;
                 }
             }
             
             //If we can get it and the inventory isn't full.
             if (getit && playersinv.Contents.length < playersinv.MaxContent)
             {
                 playersinv.AddItem(this.transform);
                 MoveMeToThePlayer(playersinv.itemHolderObject);//moves the object, to the player
             }
             else if (playersinv.Contents.length >= playersinv.MaxContent)
             {
                 Debug.Log("Inventory is full");
             }
         }
     }
     
     //Moves the item to the Players 'itemHolderObject' and disables it. In most cases this will just be the Inventory object.
     function MoveMeToThePlayer(itemHolderObject : Transform)
     {
         canGet = false;
         
         //gameObject.SetActive(false);    It's normally best to disable the individual components so we can keep item effects and update functions alive.
         
         if (GetComponent(MeshRenderer) != null)
         {
             GetComponent(MeshRenderer).enabled = false;
         }
         
         if (GetComponent(Collider) != null)
         {
             GetComponent(Collider).enabled = false;
         }
         
         GetComponent("Item").enabled = false;
         
         transform.parent = itemHolderObject;
         transform.localPosition = Vector3.zero;
     }
     
     //Drops the Item from the Inventory.
     function DropMeFromThePlayer(makeDuplicate : boolean, numInStack : int)
     {
     
         if (makeDuplicate == true) //We use this if the object is not stacked and so we can just drop it.
         {
             canGet = true;
             gameObject.SetActive(true);
             
             if (GetComponent(MeshRenderer) != null)
             {
                 GetComponent(MeshRenderer).enabled = true;
             }
             
             if (GetComponent(Collider) != null)
             {
                 GetComponent(Collider).enabled = true;
             }
         
             GetComponent("Item").enabled = true;
             
             transform.parent = null;
             DelayPhysics();
         }
         else //If the object is stacked we need to make a clone of it and drop the clone instead.
         {
             canGet = true;
             clone = Instantiate(gameObject, DropPos.transform.position, DropPos.transform.rotation);
             clone.rigidbody.AddRelativeForce(0, 100, 300);
             canGet = false;
             clone.SetActive(true);
             
             if (clone.GetComponent(MeshRenderer) != null)
             {
                 clone.GetComponent(MeshRenderer).enabled = true;
             }
             
             if (clone.GetComponent(Collider) != null)
             {
                 clone.GetComponent(Collider).enabled = true;
             }
         
             clone.GetComponent("Item").enabled = true;
             clone.GetComponent(Item).stack = numInStack;
             clone.transform.parent = null;
             clone.name = gameObject.name;
         }
     }
     
     function DelayPhysics ()
     {
         if (playersinv.transform.parent.collider != null && collider != null)
         {
             Physics.IgnoreCollision(playersinv.transform.parent.collider, collider, true);
             yield WaitForSeconds (1);
             Physics.IgnoreCollision(playersinv.transform.parent.collider, collider, false);
         }
     }
     
     //Drawing an 'I' icon on top of the Item in the scene to keep organised.
     function OnDrawGizmos ()
     {
         Gizmos.DrawIcon (Vector3(transform.position.x, transform.position.y + 1, transform.position.z), "ItemGizmo.png", true);
     }
 
 Inventory.js:
 
     //This is the central piece of the Inventory System.
     
     
     var Contents : Transform[]; //The content of the Inventory
     var MaxContent : int = 12; //The maximum number of items the Player can carry.
     
     var DebugMode = false; //If this is turned on the Inventory script will output the base of what it's doing to the Console window.
     
     private var playersInvDisplay : InventoryDisplay; //Keep track of the InventoryDisplay script.
     
     static var itemHolderObject : Transform; //The object the unactive items are going to be parented to. In most cases this is going to be the Inventory object itself.
     
     @script AddComponentMenu ("Inventory/Inventory")
     
     
     //Handle components and assign the itemHolderObject.
     function Awake ()
     {
         itemHolderObject = gameObject.transform;
         
         playersInvDisplay = GetComponent(InventoryDisplay);
         if (playersInvDisplay == null)
         {
             Debug.LogError("No Inventory Display script was found on " + transform.name + " but an Inventory script was.");
             Debug.LogError("Unless a Inventory Display script is added the Inventory won't show. Add it to the same gameobject as the Inventory for maximum performance");
         }
     }
     
     //Add an item to the inventory.
     function AddItem(Item:Transform)
     {
         var newContents = new Array(Contents);
         newContents.Add(Item);
         Contents=newContents.ToBuiltin(Transform); //Array to unity builtin array
         
         if (DebugMode)
         {
             Debug.Log(Item.name+" has been added to inventroy");
         }
         
         //Tell the InventoryDisplay to update the list.
         if (playersInvDisplay != null)
         {
             playersInvDisplay.UpdateInventoryList();
         }
     }
     
     //Removed an item from the inventory (IT DOESN'T DROP IT).
     function RemoveItem(Item:Transform)
     {
         var newContents=new Array(Contents);
         var index=0;
         var shouldend=false;
         for(var i:Transform in newContents) //Loop through the Items in the Inventory:
         {
             if(i == Item) //When a match is found, remove the Item.
             {
                 newContents.RemoveAt(index);
                 shouldend=true;
                 //No need to continue running through the loop since we found our item.
             }
             index++;
             
             if(shouldend) //Exit the loop
             {
                 Contents=newContents.ToBuiltin(Transform);
                 if (DebugMode)
                 {
                     Debug.Log(Item.name+" has been removed from inventroy");
                 }
                 if (playersInvDisplay != null)
                 {
                     playersInvDisplay.UpdateInventoryList();
                 }
                 return;
             }
         }
     }
     
     //Dropping an Item from the Inventory
     function DropItem(item)
     {
     var numInStack : int;
     
         gameObject.SendMessage ("PlayDropItemSound", SendMessageOptions.DontRequireReceiver); //Play sound
         
         if (item.stack == 1) //Drop item
         {
             RemoveItem(item.transform);
         }
         else //Drop from stack
         {
             numInStack = item.stack;
             item.stack -= item.stack;
             RemoveItem(item.transform);
             makeDuplicate = false;
         }
         
         item.DropMeFromThePlayer(makeDuplicate, numInStack); //Calling the drop function + telling it if the object is stacked or not.
         
         if (DebugMode)
         {
             Debug.Log(item.name + " has been dropped");
         }
     }
     
     //This will tell you everything that is in the inventory.
     function DebugInfo()
     {
             Debug.Log("Inventory Debug - Contents");
         items=0;
         for(var i:Transform in Contents){
             items++;
             Debug.Log(i.name);
         }
         Debug.Log("Inventory contains "+items+" Item(s)");
     }
     
     //Drawing an 'S' in the scene view on top of the object the Inventory is attached to stay organized.
     function OnDrawGizmos ()
     {
         Gizmos.DrawIcon (Vector3(transform.position.x, transform.position.y + 2.3, transform.position.z), "InventoryGizmo.png", true);
     }
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

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

18 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

Related Questions

Access parents other child from other other child 1 Answer

set all the children of one object inside the same array 1 Answer

accessing a child 1 Answer

access vector variable from script 1 Answer

Access is denied in Windows 8 App 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