- Home /
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;
}
}
}
}
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.
Do all these objects have a big trigger radius such that the player is in it?
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;
}
}
}
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()
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.
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());
}
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()); } }
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.
I know how to use "this" keyword. I just thought you were trying to explain something else.
Your answer
Follow this Question
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