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 :)
Can you post the exact error you are seeing to streamline the discovery process? :)
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)
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();
}
Thank you so much for helping me, but this doesn't seem to fix it. Still getting the same error :(
and you assigned the Game$$anonymous$$anager for both of your players in the inspector?
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612113309im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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