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 Yog0 · Feb 18, 2014 at 10:14 PM · gameobjectraycastvariablestoring

Script that stores gameobject hit by raycast keeps getting a NullReferenceException error.

I've looked at other threads about storing gameObjects as variables when hit by a ray, and I believe a duplicated the answers correctly, but I can't get this script to work.

The idea is to have one box above another, each one with this script attached. The boxes then raycast and store each other as variables so another function can edit their properties.

Lines 26 and 37 keep giving me a NullReferenceException: object reference not set to an instance of an object.

 var boxColor : Color;
 private var boxUp : GameObject;
 private var boxDown : GameObject; 
 
 
 function Start () 
 {
     renderer.material.color = boxColor;
     
     UpBoxRay ();
     DownBoxRay ();
 }
 
 function Update ()
 {
 
 }
 
 function UpBoxRay ()
 {
     var hit : RaycastHit;
     
     if (Physics.Raycast (transform.position, Vector3.up, 100))
     {
         renderer.material.color = Color.magenta;
         boxUp = hit.collider.gameObject;
     }
 }
 
 function DownBoxRay ()
 {
     var hit : RaycastHit;
     
     if (Physics.Raycast (transform.position, Vector3.down, 100))
     {
         renderer.material.color = Color.green;
         boxDown = hit.collider.gameObject;
     }
 }

I have a suspicion that my problem may be related to using the same script on multiple objects. When I take one box with the script on it and put an default sphere on the top and the bottom I only get one null reference.

Anyway, I'm stumped.

Comment
Add comment · Show 4
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 Graham-Dunnett ♦♦ · Feb 18, 2014 at 10:17 PM 0
Share

Lines 27 and 38 are just closing braces. Did something get missed when you uploaded your code? But anyway, where is hit initialised?

avatar image getyour411 · Feb 18, 2014 at 10:44 PM 0
Share

Speaking to your suspicion - no, that's the heart and soul of scripting in Unity there's nothing wrong with using a single script on multiple objects (the opposite would be a nightmare).

avatar image Maui-M · Feb 18, 2014 at 10:51 PM 0
Share

I would make sure the objects in question have active colliders on them.

avatar image Yog0 · Feb 18, 2014 at 11:57 PM 0
Share

Lines 27 and 38 are just closing braces

$$anonymous$$y working copy had one extra line in it. I meant 26 and 37, the hit.collider.gameObject line. I edited the main post for that

I would make sure the objects in question have active colliders on them.

There are box colliders on everything.

2 Replies

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

Answer by Yog0 · Feb 19, 2014 at 12:20 AM

Similarly to what JeffreyD said. Changing lines 24 and 34 to

if (Physics.Raycast (transform.position, Vector3.up, hit, 100))

seemed to do it.

Having an out before the hit would return an error. Also, I'm not sure what the F after the ray length does, but that got me on the right track.

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 Linus · Feb 19, 2014 at 12:36 AM 0
Share

Your answer is not the correct answer. Jeffreys answer is the correct answer. Yours should be a comment to his answer. Nitty picking, but don't wanna mes with karma, lesson learned in last life.

avatar image Yog0 · Feb 19, 2014 at 12:52 AM 0
Share

I debated the same thing, but while his answer has the correct reasoning, the code does not function. If someone finds this thread, I felt it would be more useful to see the working code, and then the reasoning, so I made my own post referencing his.

As for karma, there are more important things than internet points.

avatar image JeffreyD · Feb 19, 2014 at 08:36 AM 0
Share

$$anonymous$$y intent (and, I believe, is the nature of an answer) was to point you in the right direction (or in this case show you your error (i.e., missing "out hit")) and where to look for a solution not write your code for you. All I did was cut an paste the second example in the reference as indicated.

Don't worry about the $$anonymous$$arma. $$anonymous$$arma has a way of working itself out, both on the internet and in life. You get what you give... What goes around comes around, etc... Right?

Anyway, I'm glad my answer helped and pointed you in the right direction. $$anonymous$$aybe someday you'll point me in the right direction with my code. Happy coding!

avatar image
1

Answer by JeffreyD · Feb 18, 2014 at 11:00 PM

Hit is defined but nothing is loaded into it so hit.collider.gameObject causes a NUL reference. At least that is my guess.

It has to do with how the raycast call is made. Here is the reference. http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

So perhaps you want the second example

if (Physics.Raycast(transform.position, -Vector3.up, out hit, 100.0F))

Now hit should have something in it.

It may be helpful to use Debug.Log("hit is ==>" + hit); to see what is happening.

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 Yog0 · Feb 19, 2014 at 12:25 AM 0
Share

That helped. Thanks.

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

23 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

Related Questions

Assigning current color to a variable for fade out (C#) 0 Answers

Communication between objects and other scripts, variables and properties 1 Answer

Game Object referencing from another script 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Public variable in script different for every game object. 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