- Home /
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...

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?
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.
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);
}
Your answer
Follow this Question
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