- Home /
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.
Lines 27 and 38 are just closing braces. Did something get missed when you uploaded your code? But anyway, where is hit
initialised?
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).
I would make sure the objects in question have active colliders on them.
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.
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.
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.
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.
$$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!
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.
Your answer
Follow this Question
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