- Home /
Other
pick up item script issue
im trying to make it so that when you are looking at the torch you can pick it up however when i press E any where in the level the torch is picked up, its the same button to open doors but if i press E anywhere it doesnt open the doors it only picked up the torch.
Answer by MrRightclick · May 08, 2019 at 07:12 AM
You are setting CanGetTorch to true as soon as you first look at it, and never setting it to false again after that, which causes you to pick it up even if you're not looking at it after that. This also causes you to be able to pick it up anywhere on the scene after looking at it once.
I don't see any code for actually pressing a button to open doors even with the DoorCanOpen set to true, so can't comment on that.
In case you can pick the torch up even without looking at it, I'd also check the torch collider settings to make sure there are no colliders that are excessively large etc.
Also a tiny performance improving suggestion: I would also, instead of calling raycast every single update for simple things like activating a single item, prefer to call the raycast when pressing the activation button and checking for whatever is in the way, then calling the activation method on that item (be it a door or torch or anything else).
Answer by darkStar27 · May 08, 2019 at 07:28 AM
Rather then using Input class you can simply use OnMouseOver() to check if the pointer is over the gameObject.
Then check if the gameObject is the one that you wanna pick and pick it.
void Awake () {
// PlayerCasting uses Physics.Raycast to and updates distanceFromTarget float value.
distance = PlayerCasting.distanceFromTarget;
}
void Update () {
distance = PlayerCasting.distanceFromTarget;
if (!tourchPicked && Input.GetButtonDown("Action"))
{
if (distanceValue)
{
StartCoroutine("PickTourch");
}
}
}
IEnumerator PickTourch() {
// Write the code to Pick tourch here:
// YOu can play animation etc.
}
void OnMouseOver () {
if (distance <= 2.0f)
{
distanceValue = true;
pickTouurchText.text = "PICK TOURCH";
}
}
void OnMouseExit () {
distanceValue = false;
pickTouurchText.text = "";
}
Hope this Helps!
P.S. The PlayerCasting class contain this fuction:
private void Update () {
if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
{
distanceFromTarget = hit.distance;
}
}
Answer by jono56667 · May 08, 2019 at 11:06 AM
thanks for the answers guys unfortunately while trying to fix this myself i've gotten it to a slightly broken point :/ cant pick up the torch at all now, however i think i know how to fix it but i need to access a variable from another script which im not sure how to do (im super new to this)
Easiest way to access a variable in another gameobject is by using GetComponent $$anonymous$$ake the variable you need to access public, then in another script, use GetComponent to find the script your variable is in and check for the variable there.
$$anonymous$$yScriptWithVariable myScript = OtherGameObject.GetComponent<$$anonymous$$yScriptWithVariable>();
var variable = myScript.myVariable;
its not letting me upload a photo but what im trying to do is access the CanGetTorch which is a bool in my RayCast script from the interactions script? im really bad with wording
Depends on how you have set these scripts up. Are they on the same gameObject (for example, the player object)?
If the scripts are on the same gameobject, you can just add a public variable for the RayCast script to your interaction script like so:
public RayCastScript rayScript;
Then drag the RayCastScript component in the inspector to the new RayCastScript slot on the interaction script component.
Then you can access the variable you need like so:
bool canGetTorch = rayScript.CanGetTorch;
Follow this Question
Related Questions
Spawn script on mesh crashes Unity 0 Answers
How to move the main camera in Google Cardboard? 0 Answers
UI Text showing weird string value? 1 Answer
How to access scripts in other game object with raycasts? 2 Answers
Vertical distance indication 1 Answer