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 /
This question was closed May 09, 2019 at 11:11 AM by jono56667 for the following reason:

Other

avatar image
0
Question by jono56667 · May 08, 2019 at 02:53 AM · scripting problemraycastpickup

pick up item script issue

alt text 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.

capture.jpg (56.7 kB)
capture2.jpg (57.0 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

3 Replies

  • Sort: 
avatar image
0

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).

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 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!

Comment
Add comment · Show 1 · 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 darkStar27 · May 08, 2019 at 07:30 AM 0
Share

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;
         }    
     }
avatar image
0

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)

Comment
Add comment · Show 5 · 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 MrRightclick · May 08, 2019 at 11:14 AM 0
Share

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;
avatar image jono56667 MrRightclick · May 08, 2019 at 11:25 AM 0
Share

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

avatar image MrRightclick jono56667 · May 08, 2019 at 11:31 AM 0
Share

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;
Show more comments

Follow this Question

Answers Answers and Comments

218 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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