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 GWJustin · Jul 13, 2012 at 06:55 AM · destroyinventorydisableinvisiblehide

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.

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 whydoidoit · Jul 13, 2012 at 07:29 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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).

Comment
Add comment · Show 4 · 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
avatar image whydoidoit · Jul 13, 2012 at 07:37 AM 0
Share

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;
   }
avatar image GWJustin · Jul 13, 2012 at 06:23 PM 0
Share

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?

avatar image whydoidoit · Jul 13, 2012 at 08:52 PM 0
Share

Not sure on the active property, I always use SetActiveRecursively() but that just sets active in any case!

avatar image whydoidoit · Jul 13, 2012 at 08:53 PM 0
Share

You had better cache things if you use active as they cannot be found after.

avatar image
0

Answer by jc_lvngstn · Jul 13, 2012 at 08:48 PM

Would SetActiveRecursively() give you what you want?

link text

Maybe disable it, and in a moment or two just destroy it, if that meets your needs.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


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