Sounds like you wrote a bug… and that means… time to start debugging!
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
If you are working from a tutorial, remember these two steps for tutorials and / or example code:
do them perfectly, to the letter (zero typos, including punctuation and capitalization)
stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.
You absolutely NEVER need to post a NullReferenceException! You need to go fix it.
The basic 3 steps are:
Identify what is null ← any other action taken before this step is WASTED TIME
Identify why it is null
Fix that.
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
NullReference is the single most common error while programming. Fixing it is always the same.
also known as: Unassigned Reference Exception
also known as: Missing Reference Exception
also known as: Object reference not set to an instance of an object
Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!
Some notes on how to fix a NullReferenceException error in Unity3D:
You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:
drag it in using the inspector
code inside this script initializes it
some OTHER external code initializes it
? something else?
This is the kind of mindset and thinking process you need to bring to this problem:
The script looks okay. Although it does require that the user holds down the E key to carry the object around. And the object to be carried also needs to have a rigidbody component.
The thing is, I created object and set the rigibody component and when I start (and holding E is necessery, the half-life/VoTV/ any other game that have physics) in gives me the null.
Well the script is fine and so your scene setup is causing the problem. Perhaps you have something attached to your player that is blocking the raycast from the camera. A gun perhaps?. Insert a Debug.Log(hit.transform.name); at line 27 to see if the raycast is hitting the object you wish to carry and not some other object in the scene.
If a raycast starts from within an object then the object will be ignored and so it’s okay to start a raycast from inside of the player’s capsule collider.
Insert a Debug.Log(hit.transform.name); at line 27 to see which object is being hit by the raycast when looking at the object you wish to carry.