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 RVDL_IT · Apr 17, 2017 at 01:55 PM · positionobjectgravitytagonly

Tag in code only responds to 1 object.

I made a code to pick up some items and there are some mistakes .

  1. Whatever item I try to pick up, it only responds to 1 item (if I try to pick up item 1 or 2, it always moves item 1).

  2. Whenever I try to pick up the only item that moves from the first problem the gravity turns off like it should, but if I try to pick up another item the gravity doesn't turn off.

Any help is much appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class PickUp : MonoBehaviour {
     
     // The variables for the positions.
         Offset to the character.
             float OffsetPositionX = 1;
             float OffsetPositionY = 1;
             float OffsetPositionZ = 1;
         // Relative position to the character.
             float CharacterPositionX;
             float CharacterPositionY;
             float CharacterPositionZ;
   
     // The Player and the tagged objects.
         public GameObject CarryableObject;
         public GameObject Character;
   
     void Start() {
         // The origin of the objects.
             CarryableObject = GameObject.FindWithTag("Carryable");
             Character = GameObject.Find("Character");
         // The code for determining the position (Put in note mode to deactivate but keep it).
             CharacterPositionX = Character.transform.position.x + OffsetPositionX;
             CharacterPositionY = Character.transform.position.y + OffsetPositionY;
             CharacterPositionZ = Character.transform.position.z + OffsetPositionZ;
   }
     
     void OnTriggerStay(Collider other) {
         if(Input.GetKeyDown("e")) {
             // The position relative to the character.
                 CarryableObject.transform.position = new Vector3(CharacterPositionX, CharacterPositionY, CharacterPositionZ);
             // The rotation relative to the character.
                 CarryableObject.transform.rotation = Character.transform.rotation;
         }
         if(Input.GetKeyDown("e")) {
             // The gravity.
                 other.attachedRigidbody.useGravity = false;
         }
     }
 }



The position the only moving item goes to: alt text

Also, I think this might have something to do with the entire game and not just the code so here is a link to my game in the google drive. link to the game files

unity-forum-gravity-pickup-code-screenshot.png (180.2 kB)
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
1
Best Answer

Answer by Landern · Apr 17, 2017 at 01:59 PM

First and foremost your script is function as you've written it.

Line 22:

 CarryableObject = GameObject.FindWithTag("Carryable");

The GameObject.FindWithTag method/function will return the first Found Tag with whatever name you specify, this is in the documentation ("Returns one active GameObject tagged tag. Returns null if no GameObject was found.")

If you'd like to get all GameObjects with a particular tag then you would want to use GameObject.FindGameObjectWithTag, in this case it will return an Array of GameObject and you would have to parse through the array for the one you've got. Lastly you could and probably should use RayCasting to pickup a particularly tagged object from the RayCast hit.

Comment
Add comment · Show 2 · 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 RVDL_IT · Apr 17, 2017 at 05:09 PM 0
Share

I tried to use GameObject.FindGameObjectWithTag but it still gave me the same effect. I only pick up the knife.

avatar image RVDL_IT · Apr 17, 2017 at 07:52 PM 0
Share

The box collider is a temporary solution.

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

Turret floats away and pushes everything away from it 2 Answers

Getting the position of an object's specific tri 1 Answer

Object disappear when position changes at runtime 0 Answers

Destroy objects by clicking on them 1 Answer

Swap objects with same tag? 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