Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

Raycast not changing value 0 Answers

Bounds and extents of a tilemap collider across differing y axes, Tilemap collider bounds 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

Not all paths return a value 1 Answer

Strange Raycast problem 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges