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 /
  • Help Room /
avatar image
0
Question by jojoras · Mar 18, 2016 at 12:51 PM · keyboxpick up

pick a key inside a box

i want to pick up a key inside a box when its opened. i have already implemented the scripts for picking up normal keys and opening the box but when i combine those two and adding an if statement in key script (if box.IsOpen ==true) it doesnt work. IsOpen is a var in box script that changes when box opens and I have already included box gameobject in key script.

Comment
Add comment · Show 7
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 Brocccoli · Mar 18, 2016 at 01:06 PM 0
Share

I don't have a specific answer for you, just some advice.

It doesn't sound right, from a design perspective, for there to be a box object on the key. The key shouldn't care where it is, it just cares about being able to be picked up and actually being picked up.

The box may control some of the properties of the key (being able to be picked up) but the key shouldn't care at all where it is contained. Whether its in a box, a secret compartment, buried in dirt, etc.

avatar image jojoras Brocccoli · Mar 18, 2016 at 01:23 PM 0
Share

yeah but how can I implement it so it can be picked only if the box is opened? I have one box collider for box so player first has to open it when triggered and then a second collider for the key and player can pick it up after he opens the box. that's why I have the box object on the key. If i dont have it, then by pressing E, it would both open box and pick up key at the same time :/

avatar image ThomasVandenberghe jojoras · Mar 18, 2016 at 02:58 PM 1
Share

Something along the lines of the key inheriting from an PickupObject which has a boolean (CanPickUp), the box can then contain a list of different pickupobjects which have the boolean CanPickUp to false. Once the box is opened you can set the list of objects to CanPickUp = true (with or without taking into account the animation delay for opening the box)

Having the key inheriting from a pickupobject would enable the box to store different objects as well without much coding.

avatar image cbjunior · Mar 18, 2016 at 02:04 PM 0
Share

I can't tell you for certain without getting more information, but it sounds to me like you're drastically overcomplicating what should be a fairly simple problem. In most cases that I can think of the box and key won't even need to be aware of each other, but I need to know more to really help. How are you detecting if the player can interact with the box? Raycasts or just simple proximity? What is the camera perspective, top-down, first person, etc.? The scripts for the box and key would be helpful as well.

avatar image jojoras cbjunior · Mar 18, 2016 at 02:37 PM 1
Share

its first person. I have one box collider for box so player first has to open it when triggered and then a second collider for the key and player can pick it up after he opens the box. in box script if player triggered the collider and press E the box opens and the var IsOpen changes to true. And then in key script I have if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) && playerNextTo$$anonymous$$ey == true && box.IsOpen==true) { The$$anonymous$$ey.active = false; }

avatar image Soraphis jojoras · Mar 18, 2016 at 05:01 PM 1
Share

shouldn't it be "The$$anonymous$$ey.SetActive(false)"

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Brocccoli · Mar 18, 2016 at 04:46 PM

Expanding on @ThomasVandenberghe's comment:

If you have an Interface:

 interface PickupObject{
   void allowPickup();
   PickupObject pickup();
 }

and a Box class

 class Box{
   private PickupObject[] objectsInBox;
   public Box(PickupObject[] objectsInBox)
   {
     this.objectsInBox = objectsInBox;
   }
 
   public void Open()
   {
     foreach(PickupObject pickupObject in objectsInBox)
     {
       pickupObject.allowPickup();
     }
   }
 }

Where the allowPickup() method will set the (private) boolean value on the PickupObject.

The player should never know whether or not the object is able to be picked up, they should just attempt to pick up the object (via pickup()) and let the PickupObject handle the rest. Either allowing the player to pick it up, or not allowing the player to pick it up.

You can easily implement the functionality you describe without the Key ever knowing where it is, and the box not ever caring what objects it contains.

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

7 People are following this question.

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

Related Questions

Parameters of Physics2D.BoxCast are unclear in the documentation 1 Answer

How can I use on another scene the key bindings? 0 Answers

Pickup Script Problem 1 Answer

Box wont collide with anything when picked up 0 Answers

How do you keep keystates between scenes? 1 Answer


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