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 VColson · Mar 16, 2017 at 01:18 PM · 2dcollisioninheritance

Object reference not set to an instance of an object

I know this is a common error and I tried to find a solution reading the answers on others having this problem but I have not yet found it. And since this is an assignement wich needs to be done tommorow I am asking my specific problem here. So first everything worked well but I had to convert all my code so that it used inheritance. When I converted all the code everything worked fine except my local multiplayer mode where players on the same computer can play against eachother. The error I get is : Object reference not set to an instance of an object. The scripts wich may contain the problems :

-The snowball script

 using UnityEngine;
 using System.Collections;
 
 
 public class Snowball : MonoBehaviour {
 
     public float ballSpeed;
     public GameObject snowBallEffect;
     private Rigidbody2D theRB;
 
     void Start() {
         theRB = GetComponent<Rigidbody2D>();
      
     }
     void Update() {
         //this iis for that the snwobal will move.
         //and that the snowball move as fast as the speed that is set 
         //for the snowball.
         theRB.velocity = new Vector2(ballSpeed * transform.localScale.x, 0);
 
     }
 
 
 }

-The snowballplayer script (wich is on the snowball that the player uses)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SnowballPlayer : Snowball {
     //this is for when the snowball you throwed hits a enemy
     //that he will lose a life.
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Enemy")
         {
             other.gameObject.GetComponent<Enemy>().HurtEnemy();
         }
 
         //this one is for when player 2 hits player 1
         //player 1 will lose 1 life.
         else if (other.name == "player1")
         {
             other.gameObject.GetComponent<GameManager>().HurtP1();
         }
 
         //this one is for when player 1 hits player 2
         //player 2 will lose 1 life.
         else if (other.name == "player2")
         {
             other.gameObject.GetComponent<GameManager>().HurtP2();
         }
         Instantiate(snowBallEffect, transform.position, transform.rotation);
         Destroy(gameObject);
     }
 }


-The gamemaneger script (wich is being refferenced to for hurting the other player)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class GameManager : MonoBehaviour {
 
     //this script is for the 1v1.
 
     public GameObject player1;
     public GameObject player2;
 
     public int P1Life;
     public int P2Life;
 
     public GameObject p1Wins;
     public GameObject p2Wins;
 
     public GameObject[] p1Sticks;
     public GameObject[] p2Sticks;
 
     public AudioSource hurtSound;
 
     public string mainMenu;
 
     void Update()
     {
         //when player 1 has no lifes anymore he lose.
         //This will show that player 2 to wins. 
         if (P1Life <= 0)
         {
             player1.SetActive(false);
             p2Wins.SetActive(true);
         }
         //when player 2 has no lifes anymore he lose.
         //This will show that player 1 to wins. 
         if (P2Life <= 0)
         {
             player2.SetActive(false);
             p1Wins.SetActive(true);
         }
         //this will show the screen for going back or replay the game.
         if (Input.GetKeyDown(KeyCode.R))
         {
             SceneManager.LoadScene(SceneManager.GetActiveScene().name);
         }
         if (Input.GetKeyDown(KeyCode.Escape))
         {
             SceneManager.LoadScene(mainMenu);
         }
     }
     public void HurtP1()
     {
         //this is for when the player 1 gets hit he will lose a life.
         P1Life -= 1;
         //there are candysticks that represent the lifes of the player 1.
         //This will delete 1 of them when he gets hit.
         for(int i = 0; i < p1Sticks.Length; i++)
         {
             if(P1Life > i)
             {
                 p1Sticks[i].SetActive(true);
             }
             else
             {
                 p1Sticks[i].SetActive(false);
             }
         }
 
         hurtSound.Play();
     }
 
     public void HurtP2()
     {
         //this is for when the player 2 gets hit he will lose a life.
         P2Life -= 1;
         //there are candysticks that represent the lifes of the player 2.
         //This will delete 1 of them when he gets hit.
         for (int i = 0; i < p2Sticks.Length; i++)
         {
             if (P2Life > i)
             {
                 p2Sticks[i].SetActive(true);
             }
             else
             {
                 p2Sticks[i].SetActive(false);
             }
         }
         hurtSound.Play();
     }
    
 }
 

A big thank you to all that try to help :)

Comment
Add comment · Show 2
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 Deathdefy · Mar 16, 2017 at 04:39 PM 0
Share

Can you post the exact error you are seeing to streamline the discovery process? :)

avatar image VColson Deathdefy · Mar 16, 2017 at 04:50 PM 0
Share

Of course, here they are :

-When player 1 tries to shoot player 2

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

-When player 2 tries to shoot player 1

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Deathdefy · Mar 16, 2017 at 07:00 PM

Is your GameManager script on each of those players you would collider with? Thats currently how you have it coded. So you would want your GameManager to live on another object called Managers or GameManager in your scene and store a reference to it directly in your script.

 GameManager gameManager; //assign this in inspector or find in awake/start
 
  else if (other.name == "player1")
          {
              gameManager.HurtP1();
          }
  
          //this one is for when player 1 hits player 2
          //player 2 will lose 1 life.
          else if (other.name == "player2")
          {
              gameManager.HurtP2();
          }
Comment
Add comment · Show 6 · 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 VColson · Mar 16, 2017 at 07:11 PM 0
Share

Thank you so much for helping me, but this doesn't seem to fix it. Still getting the same error :(

avatar image Deathdefy VColson · Mar 16, 2017 at 08:30 PM 0
Share

and you assigned the Game$$anonymous$$anager for both of your players in the inspector?

avatar image VColson Deathdefy · Mar 16, 2017 at 09:10 PM 0
Share

I don't think it is possible to assign these via the inspector because the script is on the snowball and the snowball is a prefab wich isn't in the scene when starting up only when the player shoots.

Show more comments
Show more comments

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

157 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

Related Questions

Destorying objects on collsion 1 Answer

OnCollisionEnter2D not detecting collision 1 Answer

Storing variable collisions and editing variables in other files (2d) 0 Answers

Sprite Resizing in certain Positions 0 Answers

How to handle inventory in 2d game, via xml, no game objects (inheritance? interface?) 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