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 Hjalte · Apr 17, 2014 at 09:56 PM · c#gameobjectsmultiplepick up object

Multiple GameObjects with the same script all react but I only want one at a time.

Okay so I had a pickup script on my player which picked up stuff via raycast but that was buggy as hell for some reason so i made a script on the actual gameobject but when I click an object all objects with the script gets picked up. Now its pretty obvious why, I mean they just all change their position but anyway i wanted to know if there was some way of fixing it without making the same script thrice with a different name..

Heres the script (its not exactly mine by the way. i dont wanna take any credit i just modified it): using UnityEngine; using System.Collections;

 public class PickUpObject : MonoBehaviour {
     public Transform player;
     public GameObject playerObject;
     public float throwForce = 10;
     bool hasPlayer = false;
     bool beingCarried = false;
     
     void OnTriggerEnter(Collider other)
     {
         hasPlayer = true;
     }
     
     void OnTriggerExit(Collider other)
     {
         hasPlayer = false;
     }    
     
     void Update()
     {
         if(beingCarried){
             transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (Screen.width / 2, Screen.height / 2 + 200, 2));
             transform.LookAt(playerObject.transform.position);
             //transform.LookAt(new Vector3(player.position.x, transform.position.y, player.position.z));
             transform.Rotate(new Vector3(0, 270, 200));
 
             if(Input.GetMouseButtonDown(0)){
                 rigidbody.isKinematic = false;
                 transform.parent = null;
                 beingCarried = false;
                 rigidbody.AddForce(player.forward * throwForce);
             }
         }
         else
         {
             if(Input.GetMouseButtonDown(0) && hasPlayer){
                 rigidbody.isKinematic = true;
                 transform.parent = player;
                 beingCarried = true;
 
             }
         }
     }
 }
Comment
Add comment · Show 4
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 robertbu · Apr 17, 2014 at 10:00 PM 0
Share

Assu$$anonymous$$g your objects have colliders and you want the one to react that is being touched by the mouse, you can either:

  • Use Raycasting to detect what object is being touched and only run the code on the one that is being touched.

  • Use the On$$anonymous$$ouseDown() callback.

Lots of posts on both concepts.

avatar image getyour411 · Apr 17, 2014 at 10:16 PM 0
Share

Do all these objects have a big trigger radius such that the player is in it?

avatar image Hjalte · Apr 20, 2014 at 02:11 PM 0
Share

I changed my script to use the On$$anonymous$$ouseDown but it seems you have to actually click the object itself which makes sense but since i'm making a first person game that's very inconvenient. Is there anyway around this? should i have a mouse lock thing or? heres the script: using UnityEngine; using System.Collections;

 public class PickUpThrow : $$anonymous$$onoBehaviour {
     public Transform player;
     public GameObject playerObject;
     public float throwForce = 10;
     public bool hasPlayer = false;
     public bool beingCarried = false;
     public bool beingThrown = false;
     public float rotationAmount;
     
     void OnTriggerEnter(Collider other)
     {
         hasPlayer = true;
     }
     
     void OnTriggerExit(Collider other)
     {
         hasPlayer = false;
     }    
     
     void Update(){
         
         if (beingThrown == true) {
             transform.Rotate (0, 0, rotationAmount * Time.deltaTime);        
         }
         
         if(beingCarried){
             hasPlayer = false;
             transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (Screen.width / 2, Screen.height / 2 + 200, 2));
             transform.LookAt(playerObject.transform.position);
             //transform.LookAt(new Vector3(player.position.x, transform.position.y, player.position.z));
             transform.Rotate(new Vector3(0, 270, 200));
         }
     }
 
     void On$$anonymous$$ouseDown(){
         if(beingCarried == true){
             rigidbody.is$$anonymous$$inematic = false;
             transform.parent = null;
             beingCarried = false;
             beingThrown = true;
             rigidbody.AddForce(player.forward * throwForce);
         }
 
         if(hasPlayer == true){
             rigidbody.is$$anonymous$$inematic = true;
             transform.parent = player;
             beingCarried = true;
         }
     }
 }
 
avatar image robertbu · Apr 20, 2014 at 05:40 PM 0
Share

It is unclear what you want. Do you want the object under the mouse to react? In the center of the screen? There are other On$$anonymous$$ouse* functions, and you can use Raycasting()

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hjalte · May 11, 2014 at 03:55 PM

Hey everyone im sorry for being so terrible at explaining what i wanted but i fixed so heres my script. Thank you very much for you help. using UnityEngine; using System.Collections; public class NewPickUp : MonoBehaviour { public GameObject item; public GameObject item2; public GameObject pickUpText; private float customHeight = 0; public float throwForce = 10; public bool beingCarried = false; void Update () { Ray ray = camera.ScreenPointToRay(new Vector3 (Screen.width / 2, Screen.height / 2, 2)); Debug.DrawRay(ray.origin, ray.direction * 1.7f, Color.yellow); RaycastHit hit; if (Physics.Raycast (ray, out hit, 1.7f)) { if(hit.collider.name == "item"){ pickUpText.guiText.enabled = true; if(beingCarried){ pickUpText.guiText.enabled = false; item.transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (Screen.width / 2, Screen.height / 2+customHeight, 2)); item.transform.LookAt(transform.position); if(Input.GetMouseButtonDown(0)){ item.collider.isTrigger = false; item.rigidbody.isKinematic = false; item.transform.parent = null; beingCarried = false; item.rigidbody.AddForce(transform.position*throwForce); } } else { if(Input.GetMouseButtonDown(0)){ item.collider.isTrigger = true; item.rigidbody.isKinematic = true; item.transform.parent = transform; beingCarried = true; } } } else { if(hit.collider.name == "item2"){ pickUpText.guiText.enabled = true; if(beingCarried){ pickUpText.guiText.enabled = false; item2.transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (Screen.width / 2, Screen.height / 2+customHeight, 2)); item2.transform.LookAt(transform.position); if(Input.GetMouseButtonDown(0)){ item2.collider.isTrigger = false; item2.rigidbody.isKinematic = false; item2.transform.parent = null; beingCarried = false; item2.rigidbody.AddForce(transform.position*throwForce); } } else { if(Input.GetMouseButtonDown(0)){ item2.collider.isTrigger = true; item2.rigidbody.isKinematic = true; item2.transform.parent = transform; beingCarried = true; } } } else { if(hit.collider.name == "item3"){ pickUpText.guiText.enabled = true; if(beingCarried){ pickUpText.guiText.enabled = false; item3.transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (Screen.width / 2, Screen.height / 2+customHeight, 2)); item3.transform.LookAt(transform.position); if(Input.GetMouseButtonDown(0)){ item3.collider.isTrigger = false; item3.rigidbody.isKinematic = false; item3.transform.parent = null; beingCarried = false; item3.rigidbody.AddForce(transform.position*throwForce); } } else { if(Input.GetMouseButtonDown(0)){ item3.collider.isTrigger = true; item3.rigidbody.isKinematic = true; item3.transform.parent = transform; beingCarried = true; } } } } } } else { if(!Physics.Raycast(ray, out hit, 1.7f)){ pickUpText.guiText.enabled = false; } } } }

Yeah it might now be the most optimized script but it works so you know. Thanks again for your 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
avatar image
0

Answer by tw1st3d · Apr 20, 2014 at 05:57 PM

Take a look at the this keyword. It acts only upon the instance of its class, instead of on every class instance.

 private bool increment = false;
 private int incremented = 0;
 
 void Update()
 {
     if(Input.GetKeyDown("X")) {
         this.increment = true;
     }
     
     if(this.increment) {
         this.incremented++;
         this.increment = false;
     }
 }
 
 void OnGUI()
 {
     GUI.Label(new Rect(20,20,400,20), this.incremented.toString());
 }




Comment
Add comment · Show 3 · 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 Hjalte · Apr 26, 2014 at 06:42 PM 0
Share

im not sure how you want me to use that. Care to explain the details of this or maybe put it into my script. And also is there a reason why youre not just using UnityEngine; using System.Collections; public class Testing2 : $$anonymous$$onoBehaviour { private int incremented = 0; void Update() { if(Input.Get$$anonymous$$ouseButtonDown(0)) { this.incremented++; } } void OnGUI() { GUI.Label(new Rect(20,20,400,20), this.incremented.ToString()); } }

avatar image tw1st3d · Apr 27, 2014 at 01:19 PM 0
Share

I was merely showing the way of using the "this" keyword, and if you don't know what to do with this code, then you really shouldn't be working with C# yet. They're two extremely basic methods that are used in nearly every $$anonymous$$onoBehavior class.

avatar image Hjalte · May 11, 2014 at 03:44 PM 0
Share

I know how to use "this" keyword. I just thought you were trying to explain something else.

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Multiple constructors 2 Answers

Handling multiple tags 1 Answer

Is Merging GameObject Arrays Possible? 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