- Home /
Placing a gameObject into, and pulling out of inventory
I'd really appreciate some help with what shouldn't be a very difficult concept.
In my game when I right click gameObjects of a certain type (in my case I've created a custom class called InventoryObject) the object is added to the inventory. That's no problem. It's what happens to the gameObject in my scene that is tripping me up.
My initial thought was to just "hide" it basically - turn off all it's colliders, make the rigidbody kinematic, and disable the renderer. Well this concept is giving me issues.
I managed to disable the colliders like so:
internal var _isColliderEnabled:boolean = true;
function get isColliderEnabled():boolean
{
return _isColliderEnabled;
}
function set isColliderEnabled(value:boolean):void
{
_isColliderEnabled = value;
//get all colliders in the object
var colliders:Component[] = this.gameObject.GetComponentsInChildren(Collider);
var collider:Collider;
for(var i:int = 0;i < colliders.length; i++)
{
collider = colliders[i].collider;
//collider.enabled = !value;
collider.isTrigger = !value;
//print(i+") "+collider.gameObject);
}
}
(sorry, this window seems like it's totally screwing up my tabbing and code formatting)
Anyhow, then I turn off the rigidbody something like this:
this.rigidbody.isKinematic = !value;
My next step is to make the gameObject invisible. So I try to find the renderer in the game object and disable it. Well, for some reason there doesn't happen to be any renderer in my gameObjects. I can however find a MeshRenderer like this:
var itemRenderer:MeshRenderer = item.transform.root.gameObject.GetComponentInChildren(MeshRenderer);
Then I try to disable it:
itemRenderer.enabled = false;
Well, it's not working. My objects are still visible.
So, now I'm thinking of shifting gears and destroying the object when I place it in the inventory, and reinstantiating it when taking it out.
Can anyone offer some good insight into the best way of going about the task of placing a gameObject into, and pulling out of inventory?
Much appreciated.
A tip on code formatting - indent your code by 4 spaces or 1 tab before posting or highlight it and click the code button after pasting. I will try to fix it for you.
Answer by whydoidoit · Jul 13, 2012 at 07:36 AM
Well you are making something invisible :) Just the one thing and not what you were after though!
Presuming item is the item that you want to make invisible the easiest way is to get the mesh renderer in it like this:
var itemRenderer : MeshRenderer = item.GetComponentInChildren(MeshRenderer);
itemRenderer.enabled = false;
At the moment you are getting the first level items first mesh renderer (this could be right, but won't be if your item has a parent and it isn't the first child).
Could your items also have more than one mesh renderer? In that case:
for(var mr : $$anonymous$$eshRenderer in item.GetComponentsInChildren($$anonymous$$eshRenderer)) {
mr.enabled = false;
}
Thanks for the tips $$anonymous$$ike, on code formatting and looping through all the $$anonymous$$eshRenderers. I'll give that a go. It's a shame there isn't simply a "visible" property on GameObject that would do all this. There's a lot of room for coding efficiency improvement in the game engine.
On a related note, any idea why the "active" property has been deprecated across the board?
Not sure on the active property, I always use SetActiveRecursively() but that just sets active in any case!
You had better cache things if you use active as they cannot be found after.
Answer by jc_lvngstn · Jul 13, 2012 at 08:48 PM
Would SetActiveRecursively() give you what you want?
Maybe disable it, and in a moment or two just destroy it, if that meets your needs.
Your answer
Follow this Question
Related Questions
Hide GUI while switchcam 3 Answers
Destroying Instantiated Prefab 1 Answer
Disable/Enable Status Bar 2 Answers
Public variable not appearing in the inspector (C#) 2 Answers
Disable/Hide transform 4 Answers