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 TheCount · Jan 07, 2014 at 04:20 AM · javascriptparentvar typenull referance exeption

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.

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

1 Reply

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

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 structs 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

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

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

19 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

Related Questions

Make a simple tree 1 Answer

NullReference Exception when checking for Null 1 Answer

how to put the objects together (javascript) 1 Answer

Access parents other child from other other child 1 Answer

GetComponent searches an objects child instead of the object 2 Answers


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