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 SquirrelCoder · Jan 08, 2016 at 08:19 PM · c#collidercomponent

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

Hello everyone, I'm making a Pong clone right now and atm I am trying to make the score. Whats supposed to happen is that when you hit the right or left wall you score a point. But this error keeps popping up:

NullReferenceException: Object reference not set to an instance of an object Ball.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/Ball.cs:53)

I'm using 2 scripts, here's the first one:

using UnityEngine; using System.Collections;

public class Ball : MonoBehaviour { public float speed = 30;

 public GameManager gm;

 void Start () 
 {
     GetComponent<Rigidbody2D> ().velocity = Vector2.right * speed;

     GameObject g = GameObject.Find ("GM");
     GameManager gm = g.GetComponent<GameManager> ();
 }

 void OnCollisionEnter2D (Collision2D col)
 {
     if(col.gameObject.name == "RacketLeft")
     {
         float y = hitFactor(transform.position, col.transform.position, col.collider.bounds.size.y);

         Vector2 dir = new Vector2 (1, y).normalized;

         GetComponent<Rigidbody2D>().velocity = dir * speed;
     }

     if (col.gameObject.name == "RacketRight") 
     {
         float y = hitFactor(transform.position, col.transform.position, col.collider.bounds.size.y);

         Vector2 dir = new Vector2 (-1, y).normalized;
         
         GetComponent<Rigidbody2D>().velocity = dir * speed;
     }
 }

 float hitFactor(Vector2 ballPos, Vector2 racketPos, float racketHeight)
 {
     return(ballPos.y - racketPos.y) / racketHeight;
 }

 void OnTriggerEnter2D (Collider2D other)
 {
     if(other.gameObject.CompareTag("WallR"))
     {
         gm.countL += 1;
         gm.SetCountText();
     }

     if(other.gameObject.CompareTag("WallL"))
     {
         gm.countR += 1;
         gm.SetCountText();
     }
 }

}

Here's the second one:

using UnityEngine; using UnityEngine.UI; using System.Collections;

public class GameManager : MonoBehaviour {

 public Text rScore;
 public Text lScore;
 //public Text roundWinner;

 /*
 public GameObject ball;
 public GameObject ballSpawn;
 */

 public int countR;
 public int countL;

 void Start () 
 {
     countR = 0;
     countL = 0;
     //roundWinner.text = "";
     SetCountText ();
 }

 public void SetCountText()
 {
     rScore.text = "" + countR.ToString ();
     lScore.text = "" + countL.ToString ();
 }

 /*
 void SetCountTextL ()
 {

 }
 */

}

Thanks in advance :)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gjf · Jan 08, 2016 at 08:19 PM

in future please format ALL of your code - the error messages contain line numbers which won't match up if you don't...

your problem relates to the scope of the gm variables. i use variables and not variable because in Start() you've defined gm, which is NOT the same one that the rest of your code is using...

Comment
Add comment · Show 3 · Share
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 SquirrelCoder · Jan 08, 2016 at 08:49 PM 0
Share

@gjf so what do i do?

avatar image ShadyProductions SquirrelCoder · Jan 08, 2016 at 10:16 PM 0
Share
  void Start() {
  GetComponent<Rigidbody2D> ().velocity = Vector2.right * speed;
  gm = GameObject.Find ("G$$anonymous$$").GetComponent<Game$$anonymous$$anager> ();
  }
avatar image SquirrelCoder ShadyProductions · Jan 09, 2016 at 05:53 PM 0
Share

@ShadyProductions Thanks it worked!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make an object move to the direction in which the user faces using vr gaze interaction? 1 Answer

It lags when it collides. Help? :( 0 Answers

ui Text not responding to collider 1 Answer

[SOLVED]OverlapingSphere damage only one enemy 1 Answer

Camera following player, how do get it to stop after the player falls? 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