Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Kitty007 · Jan 02, 2019 at 05:07 PM · objectpickupdropinitialize

Can someone help me with the initialize and pick up script, please?

 Hello! I have two scripts, one to initialize an object, a second to pick it up with the player by pressing E and colliding with the initialized object. The problem is, now the object gets initialized, but if I press E again, the player picks it up, a new object gets automatically initialized and the status of the object the player carries, is null, so he can't drop it anymore. Does anybody know, what's wrong in the scripts below, I would be very thankful!

 // PLAYER SCRIPT
 
 Rigidbody rigidBody;
 
     public GameObject tempParent;
     public Transform carryPosition;
     ObjectBase object;
     bool carrying;
     Renderer rend;
     GameObject pickupObject;
 
 void Update()
     {
         if (Input.GetKeyDown(KeyCode.E))
         {
             if (pickupObject != null && !carrying)
             {
                 pickup();
                 carrying = true;
             }
 
             else if (pickupObject != null && carrying)
                 drop();
         }
     }
     
 public void pickup()
     {
         pickupObject.GetComponent<Rigidbody>().useGravity = false;
         pickupObject.GetComponent<Rigidbody>().isKinematic = true;
         pickupObject.transform.position = carryPosition.transform.position;
         pickupObject.transform.rotation = carryPosition.transform.rotation;
         pickupObject.transform.parent = tempParent.transform;
     }
 
 void drop()
     {
         pickupObject.GetComponent<Rigidbody>().useGravity = true;
         pickupObject.GetComponent<Rigidbody>().isKinematic = false;
         pickupObject.transform.parent = null;
         pickupObject.transform.position = carryPosition.transform.position;
     }
 
 public bool isPlayerCarrying()
     {
         return carrying;
     }
 
 public GameObject getPickupObject()
     {
         return pickupObject;
     }
 
 void OnTriggerEnter(Collider other)
     {
         ObjectBase object = other.GetComponent<ObjectBase>();
         if (object != null)
         {
             pickupObject = other.gameObject;
         }
     }
 
 void OnTriggerExit(Collider other)
     {
         if (pickupObject != null)
         {
             pickupObject = null;
         }
     }
     
     
     
 // ---------------- INSTANTIATE SCRIPT ----------------------
 
 public Rigidbody drug;
     public Transform spawnPoint;
     private bool isCarrying;
     private bool isNearObject = false;
 
     void Update()
     {
         isCarrying = GameObject.Find("PlayerMovement").GetComponent<PlayerAction>().isPlayerCarrying();
 
         if (!isCarrying && isNearObject && Input.GetKeyDown(KeyCode.E))
         {
             Instantiate(drug, spawnPoint.position, spawnPoint.rotation);
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         isNearObject = true;
     }
 
     void OnTriggerExit()
     {
         isNearObject = false;
     }
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 JxWolfe · Jan 02, 2019 at 09:21 PM 0
Share

what is the purpose of the Instantiate script?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vilhien · Jan 03, 2019 at 01:05 AM

You pickup with e, on trigger hit bool&&, then initiate pickup, checks for object script, carries if so,..

The object is parented to the player, trying to visualize I assume this is something actually being carried. Problem seems that your objectbase script needs a bool set to tell if the instantiated object has been picked up, and I'm sorry to assume, a way to drop to items as well, if not already wrote in. Otherwise..

I think what your code is doing is exactly what your telling it to do, I hate that.. I know right.. I hate my code sometimes.. Erk, erm so I think,

You're setting another object that can be picked up to replace the first, thus nulling the object.

You might have a simple fix with writing in a bool statement for objects in your baseobject script for when they're picked up, and when they're dropped. That might fix it. Hope that might help!

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

104 People are following this question.

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

Related Questions

How to pick / drop object 1 Answer

Picking up a box 2 Answers

Item pickup and drop function 2 Answers

Pickup script error 1 Answer

Why does my CharacterController Immediately Drop When Falling from an Object? 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