1

I am following a tutorial series.

I have come across a problem. I understand what the error message:

Object reference not set to an instance of an object

means, but I'm not sure why this message has appeared. I'm not sure why it cannot reference it. I am quite new to Unity.

I have also created my project in documents/UnityProjects/PongTutorial, where PongTutorial is the name of this project.

I created a folder for my Unity projects in order to keep them together. Could there be problems creating my Unity projects in a folder called UnityProjects instead of creating each project in the My Documents folder?

I have created a gameManager and attached a script to it called GameSetup, where the code within this script is:

using UnityEngine;
using System.Collections;

public class GameSetup : MonoBehaviour 
{
    Camera mainCam;
    BoxCollider2D topWall, bottomWall, leftWall, rightWall;
    Transform player1, player2;

    // Use this for initialization
    void Start () 
    {
    }

    // Update is called once per frame
    void Update () 
    {
        topWall.size = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(Screen.width * 2f, 0f, 0f)).x, 1f);
        topWall.center = new Vector2(0f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height, 0f)).y + 0.5f);
    }
}

I have cleaned, built and rebuilt Assembly-CSharp in MonoDevelop for this script, and PongTutorial in MonoDevelop also, where I am getting the warnings about mainCam and all of the variables of type BoxCollider2D.

These warnings say that they are never assigned to, so will have a null value, which is true. However, I am getting the object reference not set to an instance of an object for the topWall.size and topWall.center lines.

I have just tried to create new objects. For example, in Start I have mainCam = new Camera(), but this doesn't seem to work. In the video, the variables are visible in the Inspector for the GM or GameManager in my case. However, this is not the case for me.

3
  • Did you remember to attach instances to the topWall, etc, variables in the Unity editor? Commented Jun 17, 2014 at 16:46
  • Sorry im not entirely sure what you mean. Commented Jun 17, 2014 at 16:47
  • I have made empty GameObject's (called topWall, bottomWall, etc) in the Unity editor and have attached a BoxCollider2D component to them Commented Jun 17, 2014 at 16:48

2 Answers 2

4

You first need to make your variables public, otherwise they will not show in the inspector:

public class GameSetup : MonoBehaviour {

public Camera mainCam;
public BoxCollider2D topWall, bottomWall, leftWall, rightWall;
public Transform player1, player2;

// rest of your code...

}

Then drag some game objects (with the proper components attached to it) into the new fields in the inspector. This should fix the 'object reference not set to an instance of an object' error message. This error message basically means that you are trying to do work on an variable which has no value set.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh ... whoops :S... i forgot about the public part... as this tutorial is written in JavaScript and im obviously using C#, they have just used var and not public at all.... Thanks for the help... that was a silly mistake by me ... it works now but i am getting a seperate problem where the width of the topWall is greater than the playable game area/game screen... dw i will mark this as the correct answer... thanks again :)
2

Jan solved the issue, but I just wanted to offer an alternative in case you wanted your members to be private.

Just put the <Serialize> decoration on the members you want to show up in the inspector.

<Serialize>
Camera mainCam;
<Serialize>
private BoxCollider2D topWall, bottomWall, leftWall, rightWall;

3 Comments

Thanks for the solution but im not sure what Serialize means, as i haven't really used it before... I would normally want the members to be private, as i have been taught this is a better habit to get into and public can cause some problems or allow unnecessary access to certain variables.
The <Serialize> decoration just tells the compiler to prep this variable for serialization (conversion to a stream of bytes) which allows exporting the field from your program to other programs. A common serialization format is XML, which is what Unity uses to determine what shows up in the inspector. Since public variables are publicly accessible, you don't have to serialize in order to gain access to them.
Oh right ok, sounds like a good idea to use it then. Thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.