2D UFO Tutorial Q&A

This is the official thread for discussion, issues and Q&A for the 2D UFO project

Please use this thread if you have any questions, issues or feedback on this project.

In your first foray into Unity 2D development, create a simple UFO game that teaches you many of the principles of working with Game Objects, Components, Prefabs, 2D Physics and Scripting.

2D UFO is a learning project on our Learn Site:

The assets are available here:

1 Like

Hi, Iā€™m having a problem with the PlayerController C# scriptā€¦
I have checked my code against that from the complete pack but canā€™t see any difference where applicable, yet Iā€™m still getting: ā€œPlayerController.FixedUpdate () (at Assets/Scripts/PlayerController.cs:19)NullReferenceException: Object reference not set to an instance of an objectā€ Iā€™m at the stage of trying to get the UFO to move. Iā€™m guessing that this means that for whatever reason the script isnā€™t connecting to the object (It is set as a component to the object) also is there an IDE better than Visual Studio that I should use for C# in Unity?
Thanks
Dave

I fixed itā€¦ there must have been something wrong with my code because when I copy and pasted the section from below the video the game sorted itself out.

Awesome tutorials. Iā€™ve completed all of them :slight_smile: Itā€™s better than Roguelike for beginners

1 Like

Hi, I am new to C# and Game development, I have been watching the tutorials, and they have been very useful, so thank you very much for your hard work.

I have been trying to write a script on the UFO player movment(on microsoft Visual Studio 2015), but after i finished it, the error of ā€œobject reference not set to an instance of an objectā€ pops at the ā€œrb2d.AddForce (movement);ā€ line and i canā€™t get to control the UFO at all while trying to test play, here is the code that I wrote.

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{

    private Rigidbody2D rb2d;

    void start()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        Vector2 movement = new Vector2(moveHorizontal, moveVertical);
        rb2d.AddForce (movement);
    }
}

but, when I copied the code that was wrote under the video (by you guys) the error didnā€™t pop at all, here is the code that you guys wrote.

using UnityEngine;
using System.Collections;

public class CompletePlayerController : MonoBehaviour {


    private Rigidbody2D rb2d;    

    void Start()
    {
        rb2d = GetComponent<Rigidbody2D> ();
    }

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");

        float moveVertical = Input.GetAxis ("Vertical");

        Vector2 movement = new Vector2 (moveHorizontal, moveVertical);

        rb2d.AddForce (movement);
    }
}

I removed the ā€œpublic float speed;ā€ line because it didnā€™t matter, so my question is, why does my script get that error and why doesnā€™t your script get it, while both of them are the exact same?
Please help me with this and many thanks in advance.

Morning,
Have a look at your Start() function.
The ā€˜Sā€™ should be capitalised.

 void Start()
2 Likes

Good Morning Mr. Obo Shape, Thank you very much, I just tried your suggestion and the problem was the Capitalized ā€˜Sā€™ for sure, I had no clue that a capitalized letter could ruin every thing and make the error appear else where.
Thanks again for helping out, I will try and venture more on my Quest of Making Games, Good luck to both of us!.

Why this is happening:

You declare the variable to hold the reference to the 2D Rigidbidy with private Rigidbody2D rb2d;

Then you try but fail to assign the reference to an instance of the 2D Rigidbody - the instance attached to the player GameObject - in start, because itā€™s misspelled and should be Start.

Then you try to use the reference in rb2d.AddForce (movement);

ā€¦ but as you failed to get the reference, the error is telling you ā€œobject reference not set to an instance of an objectā€

This makes sense, because this is true!

1 Like

Glad to hear it! This is definitely more suitable for beginners than 2DRL.

Hi guys. Iā€™m having problems getting my script to work. First thing is that after saving the script I get this error in console

I donā€™t know if its significant but when I copy the code provided at the bottom of the tutorial and hit Run, the game still doesnā€™t respond to keyboard inputs. The UFO does not move when I press the directional keys on the keyboard. Hereā€™s my code

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    private Rigidbody2D rb2d;

    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    void FiexedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        Vector2 movement = new Vector2(moveHorizontal, moveVertical);
        rb2d.AddForce(movement);
    }

}

Thanks.

Heya!

I am not an expert but I did notice that you misspelled void FixedUpdate() for void FiexedUpdate().
Hope I helped.

2 Likes

Thanks Israfiyl. I didnā€™t spot that.

Hi all As you may b able to see, M getting all sorts of error even on my coding page. I wanna know what Iā€™m doing wrong, Thanx

M also leaving their links coz my images are not showing for some reason.


http://postimg.org/image/983xp7hbd/

http://postimg.org/image/49op5gea9/

Looking at these images, looks like you havenā€™t saved the script.
just wondering, can you save your script, close MonoDevelop down, and relaunch the script from unity(which in turn will relaunch Monodevelop), see if that couples back up the UnityEngine namespace, and get it playing nice :slight_smile:

2 Likes

Hi,

First off Thank You for yr prompt reply.

Ok so even without saving, when I opened my project today, I noticed there were no error detection on main panel, So I just ran the script after attaching it to the rigid body of the player and it worked but when I open to edit the Script, again there are error detection, as shown in the above picture.

Thankfully though, this time there are no error detection on main panel.

My question is, why do I get these error detection on Mono Development sheet?

I believe this is not normal, In the tutorial there were no such errors, that couldnā€™t be fixed on the sheet at the spot.

PS : while installing the Unity Software, there was electricity outage(itā€™s common here in this country) , Could it be that some files got corrupt installation because I had to finish installation in 2 intervals instead of 1? (I copied over the previous files)

Thanx in advance. Sorry if M too much of a bother !^^! .

Line endings are irrelevant in Unity. Scripting applications warn the user in case it makes a difference, but in our case it does not. As this is often a Mac / Win issue, itā€™s not something we can remove, unless we authored the scripts on a per platform basis, which - as the endings are irrelevant for unity - seems a bit silly. Usually there is a pop up or tool to automatically fox these. Either use the built in fix, or ignore the warning.

I am unclear what your current issue is. You seemed to have solved your ā€œspeedā€ issue as well. Are you still having trouble? If so, please post again.

I have seen this occur in MonoDevelop before. Usually saving and restarting MonoDevelop and/or Unity will solve it. Itā€™s not based on something you did incorrectly. And no, I donā€™t think the electricity issue is a problem.

ok Thank You, but restarting the Unity Software doesnā€™t work to fix it.

http://postimg.org/image/983xp7hbd/
Yes, Speed issue have been fixed,

My current issue is, I get many error messages on ā€œMono Developementā€ panel, where we write code, although if I save and restart, the script works fine, but The Script shouldnt be showing error messages on ā€œMono Developement panelā€. It gives error in almost every code that I write in it, all those red words mean, they have error reading.

Since my script works whenever I restart, I can live without fixing it but As you can imagine, Itā€™s a hassle not to be able to rectify yr code as yr writing it on Mono sheet.

Thanx for support.