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 thevraptor · Jul 21, 2014 at 09:09 PM · raycasttransformraycasthitguitextsetactiverecursively

Raycast Hit Not Working (JS)

Hello,

I have a script that is supposed to be able to open an airlock door. Ideally, when you mouse over the door, a gui that was previously inactive shows and says "Open Airlock". The problem is that the GUI Text "Open Airlock" does not become active when the raycast is pointed at the airlock. I have fiddled around with the script and came to the conclusion that the faulty part wasn't with the raycast but was where it was saying if (hit.transform == airlockDoor). It doesn't come up as a script error in Unity, it just doesn't work. Please help! Here is the script:

 #pragma strict
 
 var airlockDoor : Transform;
 var airlockDoorInside : Transform;
 var openAirlockText : GameObject;
 
 function Start () {
 
     openAirlockText.SetActiveRecursively(false);
 }
 
 function Update () {
 
     var hit : RaycastHit;
     var fwd = transform.TransformDirection (Vector3.forward);
     if (Input.GetKey (KeyCode.F) && Physics.Raycast (transform.position, fwd, 3) ) {
         if (hit.transform == airlockDoor) {
                 airlockDoor.SendMessage("OpenAirlock", SendMessageOptions.DontRequireReceiver);
         }
             
         if (hit.transform == airlockDoorInside)
                 airlockDoorInside.SendMessage("CloseAirlock", SendMessageOptions.DontRequireReceiver);
     }
     
     if (hit.transform == airlockDoor)
         openAirlockText.SetActiveRecursively(true);
         
     if ((hit.transform == airlockDoor) == false)
         openAirlockText.SetActiveRecursively(false);
 }

Thanks. Any help or solution to this problem, even in C#, is very much appreciated.

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

2 Replies

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

Answer by thevraptor · Jul 24, 2014 at 08:21 AM

Problem solved, everyone! Turns out that for some reason, setting game objects active under a raycast doesn't work, so I relayed it through a variable using the hit.transform and it worked like a charm! Thanks, Rutter!

 #pragma strict
 
 var airlockDoor : Transform;
 var airlockDoorInside : Transform;
 var openAirlockText : GameObject;
 var mlem : boolean = false;
 
 function Start () {
 
     openAirlockText.SetActiveRecursively(false);
 }
 
 function Update () {
 
     var hit : RaycastHit;
     var fwd = transform.TransformDirection (Vector3.forward);
     if (Physics.Raycast(transform.position, fwd, hit, 1) ) {
         mlem = true;
         print ("mlem");
         if (hit.transform == airlockDoor) {
                 openAirlockText.SetActiveRecursively(true);
         }
             
         if (hit.transform == airlockDoorInside)
                 airlockDoorInside.SendMessage("CloseAirlock", SendMessageOptions.DontRequireReceiver);
     }
     
     if (hit == true)
         print ("mlem");
         
     if (Physics.Raycast(transform.position, fwd, hit, 1) == false)
         mlem = false;
         
     if ((hit.transform == airlockDoor) == false)
         openAirlockText.SetActiveRecursively(false);
         
     if (mlem == true && hit.transform == airlockDoor)
         openAirlockText.SetActiveRecursively(true);
 }
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 rutter · Jul 21, 2014 at 09:16 PM

There are many different "overloaded" versions of the Raycast function. You can view all of them at the scripting manual, or in MonoDevelop once you start typing out the function call.

You need to call one which populates the RaycastHit data. Perhaps something like this:

 Physics.Raycast(transform.position, fwd, hit, 3)


As an aside, the raycast hit will only be populated if the raycast actually hits something (ie: the call returns "true"). It's potentially unsafe to access the hit data if you don't know for sure that it's valid.

Finally, the script may act a bit unintuitively if you've forgotten to set airlockDoor or airlockDoorInside in the Inspector.

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 thevraptor · Jul 21, 2014 at 10:08 PM 0
Share

Thanks, but I just tried that but with no luck. I do have the transforms set to the doors in the Inspector, but the GUIText still will not pop up. Is this possibly a glitch in the game engine? Or am I just stupid?

avatar image rutter · Jul 21, 2014 at 10:26 PM 0
Share

Hopefully none of the above! The physics engine is pretty complicated, with a lot of moving parts. $$anonymous$$y main suggestion is to test all of your assumptions. Do these objects have colliders attached? Are they on a layer that ignores raycasts? Are you casting to/from the exact positions you expect? Are you hitting an object other than the one you expect? Are these raycasts even being fired in the first place?

The Debug class can be very helpful. Especially the DrawLine, DrawRay, and Break functions, if you haven't tried them yet.

avatar image thevraptor · Jul 22, 2014 at 05:59 PM 0
Share

Ok, so I've tampered around with the script a little bit more, and here is the script in which I figured out what the problems were:

 var airlockDoor : GameObject;
 var airlockDoorInside : Transform;
 var openAirlockText : GameObject;
 
 function Start () {
 
     openAirlockText.SetActiveRecursively(false);
 }
 
 function Update () {
 
     var hit : RaycastHit;
     var fwd = transform.TransformDirection (Vector3.forward);
     if (Physics.Raycast(transform.position, fwd, hit, 3) ) {
         openAirlockText.SetActiveRecursively(true);
         print ("mlem");
         if (hit.transform == airlockDoor) {
                 openAirlockText.SetActiveRecursively(true);
         }
             
         if (hit.transform == airlockDoorInside)
                 airlockDoorInside.Send$$anonymous$$essage("CloseAirlock", Send$$anonymous$$essageOptions.DontRequireReceiver);
     }
     
     if (hit == true)
         print ("mlem");
         
     if ((hit.transform == airlockDoor) == false)
         openAirlockText.SetActiveRecursively(false);
 }

Now, there's two problems with this script. When it's casting the ray and returns a hit under the raycasting if, it prints mlem as it's supposed to, but doesn't make the gui text ( the open airlock text var) active. The other problem is that when I remove said print ("mlem") and leave the one at the bottom as is, it doesn't print mlem as it is supposed to. So, if the hit var doesn't work, then hit.transform can't work. The problem with the gui text not beco$$anonymous$$g active is extremely confusing to me, as it should be working. I have other gui text in the scene, and they work fine in that context. I'm also not sure why the hit var is not working.

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

RaycastHit: What is the difference between hit.transform.tag and hit.collider.tag? 1 Answer

Difference between hit.collider.gameObject vs hit.transform.gameObject 2 Answers

is hit.transform always the same as hit.transform.gameObject.transform 1 Answer

Finding a RaycastHit tag (Null Reference Exception) 2 Answers

RaycastHit info works using print() but not GUIText? 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