Hi everyone,
I am new to unity and C# programming. I am making a game in which I’ve made a gameobject “Active Avatar”. I am adding some of the properties on this gameobject through a C# script attached to it.
This script is -
using UnityEngine;
using System.Collections;
public class AddChosenAvatarProperties : MonoBehaviour {
//public Ball ball;
// Use this for initialization
//public GameObject healthsc;
void Start () {
string selectedAvatar = PlayerPrefs.GetString("SelectedAvatar");
switch (selectedAvatar)
{
case "RollerBall":
RollerBall();
break;
case "FireBall":
break;
}
}
void RollerBall()
{
// gameScript = ball.AddComponent<Ball>();
Health healthsc = gameObject.AddComponent<Health>();
healthsc.healthPoints = 1;
healthsc.respawnHealthPoints = 1;
healthsc.numberOfLives = 1;
healthsc.isAlive = true;
//healthsc.explosionPrefab = ;
healthsc.healthPoints = 1;
healthsc.LevelToLoad = "Level 1";
Damage damagesc = gameObject.AddComponent<Damage>();
damagesc.damageOnCollision = true;
damagesc.damageAmount = 10;
}
}
When I run the game I get an error -
NullReferenceException: Object reference not set to an instance of an object
GameManager.Update () (at Assets/Scripts/GameManager.cs:59)
My GameManager script is -
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
public static GameManager gm;
[Tooltip("If not set, the player will default to the gameObject tagged as Player.")]
public GameObject player;
public enum gameStates {Playing, Death, GameOver, BeatLevel};
public gameStates gameState = gameStates.Playing;
public int score=0;
public bool canBeatLevel = false;
public int beatLevelScore=0;
public GameObject mainCanvas;
public Text mainScoreDisplay;
public GameObject gameOverCanvas;
public Text gameOverScoreDisplay;
[Tooltip("Only need to set if canBeatLevel is set to true.")]
public GameObject beatLevelCanvas;
public AudioSource backgroundMusic;
public AudioClip gameOverSFX;
[Tooltip("Only need to set if canBeatLevel is set to true.")]
public AudioClip beatLevelSFX;
private Health playerHealth;
void Start () {
if (gm == null)
gm = gameObject.GetComponent<GameManager>();
if (player == null) {
player = GameObject.FindWithTag("Player");
}
playerHealth = player.GetComponent<Health>();
// setup score display
Collect (0);
// make other UI inactive
gameOverCanvas.SetActive (false);
if (canBeatLevel)
beatLevelCanvas.SetActive (false);
}
void Update () {
switch (gameState)
{
case gameStates.Playing:
if (playerHealth.isAlive == false)
{
// update gameState
gameState = gameStates.Death;
// set the end game score
gameOverScoreDisplay.text = mainScoreDisplay.text;
// switch which GUI is showing
mainCanvas.SetActive (false);
gameOverCanvas.SetActive (true);
} else if (canBeatLevel && score>=beatLevelScore) {
// update gameState
gameState = gameStates.BeatLevel;
// hide the player so game doesn't continue playing
player.SetActive(false);
// switch which GUI is showing
mainCanvas.SetActive (false);
beatLevelCanvas.SetActive (true);
}
break;
case gameStates.Death:
backgroundMusic.volume -= 0.01f;
if (backgroundMusic.volume<=0.0f) {
AudioSource.PlayClipAtPoint (gameOverSFX,gameObject.transform.position);
gameState = gameStates.GameOver;
}
break;
case gameStates.BeatLevel:
backgroundMusic.volume -= 0.01f;
if (backgroundMusic.volume<=0.0f) {
AudioSource.PlayClipAtPoint (beatLevelSFX,gameObject.transform.position);
gameState = gameStates.GameOver;
}
break;
case gameStates.GameOver:
// nothing
break;
}
}
public void Collect(int amount) {
score += amount;
if (canBeatLevel) {
mainScoreDisplay.text = score.ToString () + " of "+beatLevelScore.ToString ();
} else {
mainScoreDisplay.text = score.ToString ();
}
}
}
on line 59 as stated in the error I have playerHealth.isAlive == false , in my script I am adding isAlive boolean variable as healthsc.isAlive=true; still I am getting this error what can be the reason ? How to fix this ?