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*.SetActive(true);*
}
else
{
p1Sticks*.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*.SetActive(true);*
}
else
{
p2Sticks*.SetActive(false);*
}
}
hurtSound.Play();
}
}
A big thank you to all that try to help