• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by SpaceyJ · May 29, 2016 at 01:17 PM · c#raycastcollider2dbooleantagging

NullReferenceException: Object Reference not set to an instance of an object

I am making a 2D game in the style of pac-man. I am using a script to place "dots" in the scene (called pacdot in my script). This script is attached to a gameobect called placer which moves round the scene, placing the dots. However as my game contains a maze and therefor I have a ray-casting function that returns a boolean value based on weather the placer game object is in contact with the collider from the maze. If false is returned by the function then no dots are placed and instead the placer moves to the next location. My game contains waypoints. These waypoints are also detected by the raycasting function, however I want dots to be placed over these invisible waypoints. To do this i have given all my waypoints the tag of "Waypoint". I then detect in the ray cast function weather i have detected a waypoint, and if so the function still returns true. I am receiving the error as shown in the title however for line 16 and i do not understand what i am doing wrong. Any help would be really appreciated. :)

using UnityEngine; using System.Collections;

public class Place : MonoBehaviour {

 public GameObject pacdot;

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

 bool valid(Vector2 dir) {
     Vector2 pos = transform.position;
     
     RaycastHit2D hit = Physics2D.Linecast (pos, pos);
     if (hit.collider.gameObject.tag != "Waypoint")
         return (hit.collider == GetComponent<Collider2D> ());
     else return(true);
     
 }

 // Update is called once per frame
 void FixedUpdate () {
     if (valid(Vector2.right)) {
         if (transform.position.y > 1) {
             if (transform.position.x < 28)
                 Instantiate (pacdot, new Vector2 (transform.position.x, transform.position.y), Quaternion.identity);
             transform.Translate (1, 0, 0);
             if (transform.position.x > 28)
                 transform.Translate (-28, -1, 0);
         }
     }    
     if (valid (Vector2.right) == false) {
         transform.Translate (1, 0, 0);
 }
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Quertie · May 30, 2016 at 09:22 AM 0
Share

I'm sorry but it's hard to tell which is line 16, as your question is posted... Could you copy-paste the line that's causing the problem?

Looking at your code, the only line that looks like it can cause a problem is:

return (hit.collider == GetComponent ());

From what I understand, you are telling that function to return true if the raycast collides with the placer! - GetComponent () refers to the Collider2D attached to the placer, if there is one. So either you made a mistake and did not want the function to return true if the raycast collides with the placer, or you forgot to attach a Collider2D to the placer!

Or I am just completely wrong about this.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

169 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Not all paths return a value 1 Answer

Bounds and extents of a tilemap collider across differing y axes, Tilemap collider bounds 0 Answers

Raycast not changing value 0 Answers

For my tile puzzle game, how can i have the player cube only step on a tile once and if they step on the tile a second time the game will be lost? 2 Answers

onMouseUp 2D Issues 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges