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 /
avatar image
0
Question by PrezThompkins · Jul 07, 2011 at 11:05 PM · playerdestroyinventorypickup

Picking up Objects (from within the player's code)

I'm having trouble getting an object to pick up into the player's inventory class! I'll paste my code below... For an overview, Pickup objects have a trigger range, that the player should notice when entering. When that happens, I want the player to read if they can pick that object up, pick it up, and remove the object from the scene... I've had some coding experience, but I'm new to scripting, so the setup is still very confusing for me :(. This code should work with multiple objects, once I add more to the game... I may re-use it for some Enemy AI functions as well. Any help is appreciated!

 using UnityEngine;
 using System.Collections;
 
 public class ThePlayerInventory : MonoBehaviour 
 {
 
     public int pickupObjectsHeld;
     public bool canGet;
     GameObject Target;
     
     
     // Update is called once per frame
     void Start()
     {
         pickupObjectsHeld = 0;
         canGet = false;
     }
     
     void onTriggerStay(Collider other)
     {
         if (other.transform.tag == "PickUpObject1")
         {
             canGet = true;
             Target = other.gameObject;
     
         }
     }
      
     void Update()
     {
         onTriggerStay(Target);
         
         if (Input.GetButton("Use/PickUp"))
             {    
                 if (canGet)
                 {
                     pickupObjectsHeld += 1;
                     canGet = false;
                     Destroy(Target, 0);
                 }
             }
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GuyTidhar · Jul 08, 2011 at 04:42 AM

  1. You've ment OnTriggerStay and not onTriggerStay (upper case first letter).

  2. You should not call OnTriggerStay by yourself. It is a monobehaviour function that will be called by unity if the game object to which this script is attached to has a collider attached to it and if the "other" collider is a collider too (one or both of them need to be a rigidbody/charactercontroller).

  3. You probably need to use OnTriggerEnter and not OnTriggerStay so this is called only when the collision is first identify when objects enter it (and not while they are in it).

So your script would be something like this:

 using UnityEngine;
 using System.Collections;
 public class ThePlayerInventory : MonoBehaviour 
 {
     public int pickupObjectsHeld;
     public bool canGet;
     GameObject Target;

     // Update is called once per frame
     void Start()
     {
         pickupObjectsHeld = 0;
         canGet = false;
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.transform.tag == "PickUpObject1")
         {
             canGet = true;
             Target = other.gameObject;
         }
     }
 
     void Update()
     {
         if (Input.GetButton("Use/PickUp"))
             {   
                 if (canGet)
                 {
                     pickupObjectsHeld += 1;
                     canGet = false;
                     Destroy(Target, 0);
                 }
             }
     }
 }


IF you will have a tag for different types of pickups, say: "PuckUpObject1", "PickupObject2" etc...

You can do:

 void OnTriggerEnter(Collider other)   
 {
     switch ( other.transform.tag )
     {
     case "PickUpObject1":
     case "PickUpObject2":
     case "PickUpObject3":
         canGet = true;
         Target = other.gameObject;
         break;
     }
 }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

When all objects with a certain tag has been destroyed, load the next level! 3 Answers

Destroy trigger after a certain time 2 Answers

Line Renderer Collision Detection. 2 Answers

How can I detect if box is touching a tag named weapon? 1 Answer

2D Movement Problems 2 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