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.
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.
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.
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!.
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ā
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);
}
}
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
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.
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.