Java Adding velocity to Bullet, Object reference not set

I’m following and old tut by the tornadotwins. I was pretty sure I typed in everything right. Code is as follows.

var speed = 7.0;
var rotateSpeed = 3.0;
var bullitPrefab:Transform;

function Update ()
{
var controller : CharacterController = GetComponent.();

//Rotatearoundy- axis
transform.Rotate(0, Input.GetAxis (“Horizontal”) * rotateSpeed, 0);

//Moveforward/ backward
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis (“Vertical”);
controller.SimpleMove(forward * curSpeed);

if(Input.GetButtonDown(“Jump”))
{
var bullit = Instantiate(bullitPrefab, GameObject.Find(“spawnpoint”).transform.position, Quaternion.identity);
bullit.Rigidbody.AddForce(Transform.Forward * 2000);
}
}
@script RequireComponent(CharacterController)

This is the error I’m getting when pressing space bar. The bullit is spawned but does not move.

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs)
move around.Update () (at Assets/scripts/Character Scripts/move around.js:21)
Google search suggests code may be being read as Boo?
I’ved tried some fixes, like adding script to the bullet, but it seems to compound the problem. Any help would be appreciated.
Another thing, seems odd that monodevelope does not seem to recognize some words such as find, or identity. ( It does not highlight it like I’m seeing in the video.)

I’d like to be able to finish a few tuts, start to finish to get the hang of learning Unity, so if any one has other suggestions for tuts, let me know. also it looks like learning script is going to be very important. I’d like to spend an hour a day on a “guided project” tut, another on a Coding tut, and finally on a blender tut, preferably all working on same thing or similar subjects.
If it wasn’t apparent, I’m a Noob, and want to get good, fast. But, I’m stuck.

So thanks in advance!

and if you post the entire script the line numbers from the error should line up with what you paste into the forums.. always a good start :slight_smile:

have you checked out the official learn content?

going to guess a little that its this

var bullit = Instantiate(bullitPrefab, GameObject.Find("spawnpoint").transform.position, Quaternion.identity);
// specifically this bit
GameObject.Find("spawnpoint")

find is case sensitive, so SpawnPoint, spawnPoint and spawnpoint are all different… and something with the exact name you specify has to exist in the current scene.

failing that, have you dragged something in the “bullitPrefab” slot in the inspector?

Welcome to the forums :slight_smile:

Aside of what @LeftyRighty said, if you’re just learning to program (as in - it’s new to you anyway), I’d highly recommend to go with C# instead of UnityScript (btw - it’s not Java nor JavaScript, it’s a Unity specific crossover between JavaScript and C#).
It will be much easier to find tutorials, help on these forums and others, as well as to actually learn good practices. Loosely typed languages like UnityScript are maybe easier to use at first, but are also much easier to make mistakes with. If you’re just learning, you won’t see them and it will be a lot of time spent “chasing ghosts”.
As a side bonus, learning C# (even with “just” the Unity subset) is a greater benefit for you as a individual than UnityScript will ever be.
(in my [and quite a lot, but not all others] opinion, of course)

It would also be beneficial to find tutorials for the version of Unity you’re using (you said it’s old, I’ve checked the channel and if it’s the Make from scratch, it’s from 2014 so IIRC not for Unity5) to not learn to use things that may already be obsolete.

So I can post a code that looks a bit more like this,

var speed = 7.0;
var rotateSpeed = 3.0;
var bullitPrefab:Transform;

function Update ()
{
    var controller : CharacterController = GetComponent.<CharacterController>();

    // Rotate around y - axis
    transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);

    // Move forward / backward
    var forward = transform.TransformDirection(Vector3.forward);
    var curSpeed = speed * Input.GetAxis ("Vertical");
    controller.SimpleMove(forward * curSpeed);

    if(Input.GetButtonDown("Jump"))
    {
        var bullit = Instantiate(bullitPrefab, GameObject.Find("spawn point").transform.position, Quaternion.identity);
                                            bullit.Rigidbody.AddForce(Transform.Forward * 2000);
}
}
    @script RequireComponent(CharacterController)

It looks the same on the screen now, so it will be interesting to see if it posts correctly…

I see now that the error code points to line 20, but it seems it is in fact finding the spawnpoint. The bullet shows up, it just does not move. I have indeed placed the fireball into the bullitPrefab.

No I have not. As with many people these days, my first instinct was Youtube.

I believe what your saying. I see a lot about c# on the web… However, I would like to learn javascript, or java, or whatever at some point.

I liked the way he was walking through the tut. Its hard to pick which ones to start off with, as some people take forever to get to the point.
I liked the idea of the worm game, because it reminded me of snake. If the code is dated, it does not help me though.

Is there a way to test the code within monodevelp? I tried build practice, which returns no errors,

There are two caveats I need to point out though. First, you’re not actually learning JavaScript. You’re learning a language that is masquerading as JavaScript but has subtle differences (link below lists the differences). Second, those who previously used JavaScript have largely moved to C#. Unless you’re working on an older project you’ll almost never see it.

http://wiki.unity3d.com/index.php?title=UnityScript_versus_JavaScript

On a side note there is a third language but you need to manually add the scripts for them. Support for creating Boo scripts through the editor was removed though.

https://noobtuts.com/unity/boo-tutorial

So after reading the documentation on each of the functions in line 20, I can find no faults.
Removing line 21, resulted in no errors. The problem exists there.

Quick property accessors were removed with the release of Unity 5. It’s a common problem with older tutorials.

https://blogs.unity3d.com/2014/06/23/unity5-api-changes-automatic-script-updating/

You just need to change the line of code to use GetComponent instead.

bullit.GetComponent(Rigidbody).AddForce(Transform.Forward*2000);
1 Like

No change in the error output. Even stranger, the fireball failed to render while turning left.
Maybe theres another solution to get the same effect. Im able to spawn the fireball, it just doesnt move. Possibly a c# script could tell it to. This is a solution that was suggested in the comments of the tut, which i tried using the code provided, but nothing happened.
I tried seeing if it was a problem with the scene by placing the spawn point above the level. It did not travel in any unexpected directions, just down as it was affected by gravity. Actually its kinda cool, as the worm jumps a bit moving over the fireball, effectively climbing a stairway of fireballs.

Tried changing line to

 bullit.GetComponent(Rigidbody).AddRelitiveForce(transform.Forward*2000);

Same error.
AddForce seems to not be functioning as intended, as well as addrelativeforce

Figured it out. You were correct in that I needed to add GetComponent to the function. It was substantiated in various articles online. Thank you for that.
The transform.forward function was the culprit. The working code is as follows for anyone that’s looking for this answer.

var speed = 7.0;
var rotateSpeed = 3.0;
var bullitPrefab:Transform;

function Update ()
{
var controller : CharacterController = GetComponent.<CharacterController>();

//Rotatearoundy- axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);

//Moveforward/ backward
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);

if(Input.GetButtonDown("Jump"))
{
var bullit = Instantiate(bullitPrefab, GameObject.Find("spawnpoint").transform.position, Quaternion.identity);
bullit.GetComponent(Rigidbody).AddForce(transform.forward * 2000);
}
}
@script RequireComponent(CharacterController)

For anyone who posted on this, thank you very much. I’ll post a link on the video. Hopefully others will be able to use this. Even if it is an old video.

The verdict was I had changed the capitalization in the transform.forward function. There code was correct minus the getcomponent function. YAY!!!

bullit.GetComponent(Rigidbody).AddForce(transform.forward*2000);

Winner!!! Super Cool!!!