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 NickCh · Dec 29, 2012 at 01:18 AM · cameraraycasthit

Problem With Raycasts, Interaction Script.

Hello there. I'm trying to make an interaction script.Everytime the ray hits the object with a certain tag it will run the object's script named DoorInter.But everytime I try to ray an object, even if it's not that i get the error

NullReferenceException CameraRayRecongition.Update () (at Assets/[Assets & Prefabs]Scripts/Player/CameraRayRecongition.js:16)

This is the Code :

 var length : float = 100.0;
 // Update is called once per frame
 function Update () {
             var drawer : GameObject     =     GameObject.FindWithTag("Drawer");
             var door   : GameObject     =     gameObject.FindWithTag("Door");
             var switches:GameObject        =     GameObject.FindWithTag("Switches");
              var ray : Ray =  Camera.main.ScreenPointToRay(Input.mousePosition);
              var hit: RaycastHit;
              var crosshairScript : CrossHair = GameObject.FindWithTag("MainCamera").GetComponent(CrossHair);
              
     Debug.DrawRay (ray.origin, ray.direction * length, Color.blue);     
        if(Physics.Raycast (ray, hit,length)){
        var coll = hit.collider;
 
     if(coll == drawer.collider){
     //var inter : DoorInter = coll.GameObject.GetComponent(DoorInter);
     crosshairScript.crosshairChangingDrawer();
    // inter.Dosomething();
     }
      if(coll == door.collider){
    //  var inter2: DoorInter = coll.GameObject.GetComponent(DoorInter);
     crosshairScript.crosshairChangingDoor();
   
    // inter2.Dosomething();
     }
       if(coll == switches.collider){
     crosshairScript.crosshairChangingDoor();
   /// var inter3 : DoorInter = coll.GameObject.GetComponent(DoorInter);
    // inter3.Dosomething();
     }
  }
    }

Any ideas what should i do?

Comment
Add comment · Show 1
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 NickCh · Dec 29, 2012 at 02:15 AM 0
Share

bumping the post ;D

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by fafase · Dec 29, 2012 at 10:44 AM

First off, "bumping post" is not really the way to go. Some forum would delete you for this kind of prctice (App Hub).

Now you need to understand the difference between GameObject and gameObject as I think this is where you are wrong.

 var door   : GameObject  =   gameObject.FindWithTag("Door");

THis shoud not work as FindWithTag is a static function.

 var inter : DoorInter = coll.GameObject.GetComponent(DoorInter);

This should be

 var inter : DoorInter = coll.gameObject.GetComponent(DoorInter);

Using GameObject, you refer to the actual class. You use this for static methods like FindWithTag.

gameObject is used for instance methods like GetComponent

So when you want to do something that is inherent to the object use gameObject. If you want to do something general like finding an object in the scene, use GameObject.

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 Fattie · Dec 29, 2012 at 10:46 AM 0
Share

An outstanding explanation.

"you need to understand the difference between GameObject and gameObject as I think this is where you are wrong"

quite right...

Just to repeat, "gameObject" with a $$anonymous$$iscul "g" refers "to you" - to the thing at hand.

(Technically speaking, to the thingy to which the script is attached.)

avatar image fafase · Dec 29, 2012 at 10:51 AM 0
Share

On top of that, I now realize all those GetComponent and Find functions are in the Update. This is about to get real slow.

You should consider moving at least the first three lines in the Start so that they only happen once with the variables being global.

avatar image NickCh · Dec 29, 2012 at 01:15 PM 0
Share

I see, and yea sorry for bumping :P Thanks a lot for your information :D

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

10 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

Related Questions

How to shoot a raycast with third person 1 Answer

A node in a childnode? 1 Answer

Is object at least partly visible? 1 Answer

How do I check if the mouse is over a VISIBLE vertex? 1 Answer

Raycast hit problem for fast moving object : Unity3d 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