- Home /
Detecting gameObject from 3D rayCast
function castRays(){
var fwd = transform.TransformDirection (Vector3.right * -1);
if (Physics.Raycast (transform.position, fwd, 30)) {
if (RaycastHit.transform.gameObject.tag == 'PowerUp'){
print('powerup');
}
}
}
I get a null object reference every time I run this script? Why?
I actually now seem to be getting an instance type error also.
Answer by Benproductions1 · Jan 07, 2014 at 06:30 AM
Hello,
A couple things are wrong with your script, all of which the compiler would tell you about if you used the #pragma strict
directive and you wouldn't have such problems. Compilers are smart, so use them to their full potential!
SYNTAX
First thing I would like to mention is syntax. When you're writing code, it doesn't matter too much how you write it, as long as you're consistent with it. Of course certain styles are much, much easier to read and come back to, but you should at least write consistent code:
First thing I notice is that you're inconsistant with spacing when calling and writing functions. Make a choice between putting a space between the function name or not, don't mix and match when suited:
function name () {
call (arg1, arg2);
}
//OR
function name() {
call(arg1, arg2);
}
The second method is more common however, since it links the call to the function, rather than having it seperately.
Second is obviously indentation. Which is easier to read?
function f1() {
if (stuff1 == stuff2) {
do(things);
}
}
//OR
function f1() {
if (stuff1 == stuff2) {
do(things);
}
}
You get my point. Code on the same level should also have the same indentation...
TYPES
Unlike C#, UnityScript doesn't force you to give your variables types, however it makes code easier to read, easier to debug and makes the compiler throw most of your mistakes as errors. So you should do this:
var fwd:Vector3 = stuff();
//instead of
var fwd = stuff ();
Again, the pragma strict
directive would catch that as a syntax error and force you to specify the type.
USING REFERENCES
Like in most compiler languages, you can almost never have two references with the same name. One will not override the other and it will cause exceptions and compiler errors:
Lets say you make a script called foo
(which is compiled as a class), and then somewhere else you do:
function bar() {
var foo:int = 5;
At this point the compiler should already throw an error because you are trying to create a variable with the same name as a class, however even if it doesn't, what happens when you do:
return foo;
}
How is the compiler supposed to know which foo
to return? It could be the script (class) but it could also be the variable. Since you didn't (and can't) specify which one, the compiler throws an error (or it should).
This specifically is not a mistake you make, but it is however part of a larger mistake.
RAYCASTHIT AND RAYCAST
RaycastHit
is a struct
, kind of like a class (UnityScript script) but a little different in that it has a default value (more is different but that's for another day). On line 4 you try to use the struct
RaycastHit
as if it was a instance of the struct
. RaycastHit
refers to the struct
, not the last hit. As per the documentation the correct way of using RaycastHit
is to do something like:
var hit:RaycastHit;
if (Physics.Raycast(pos, dir, hit, dist)) {
hit.transform;
}
The reason for that, is that you need a instance of RaycastHit
in order to retrieve data from a Raycast
hit.
You might be wondering, if hit
is a variable, why don't I have to set it to anything? The reason being, that it's a struct
, just like int
, float
etc. You never have to set those either.
You might also be wondering how the function is the setting the values in hit. The reason being that in the function definition, the keyword out
is used. Think of it like this: if out
is used, then any change to the variable in the function also means it will change out of the function. This only applies to struct
s though, as they are usually passed by value, not by reference.
Now that you know all that, it's time to rewrite your code the proper way and make it work. If you don't understand anything (or even if you're just curious) there is plenty of information out there on the internet, you really just have to look. The Unity Documentation is a good place to look at. There are clear examples for many of the key features of the engine and the editor, in all three languages.
Hope this helps,
Benproductions1