The question is answered, right answer was accepted
NullReferanceExeption Unity 5.2 C# script
I have a script for an object. the objects goal is to be destroyed by the player and give the player score. the problem is that I get a nullreferenceexeption when trying to define my player.
using UnityEngine;
using System.Collections;
public class blockBehavior : MonoBehaviour
{
public int health;
public float time;
private GameObject player;
public int value;
void Start ()
{
player = GameObject.FindWithTag("Player");
}
void Update ()
{
player = GameObject.FindWithTag("Player");
if(player == null)
{
Debug.Log("cannot find the object: Player");
}
if (health <= 0)
{
player.GetComponent<playerMovement>().score += value;
Destroy(gameObject);
}
}
void FixedUpdate()
{
time -= 1;
if(time < 0)
{
Destroy(gameObject);
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "bullet")
{
health -= 1;
}
if(collision.gameObject.tag == "Player")
{
player.GetComponent<playerMovement>().score += value;
Destroy(gameObject);
}
}
}
the reference exeption seems to haft to do with
player = GameObject.FindWithTag("Player");
but I dont know why
Thanks in advance!
I dont want the player to be destroyed after a certain amount of time, I want the object (I cannot call it an enemy because I have a different object as an enemy) to be destroyed after a long time (3000 seconds) so that it will not make the game lag and so I have room for other objects in the scene. my player if it collides with the object will destroy the object and give it score, while if you shoot it with the player's bullet (not to be confused with the enemy's bullet) it will decrease its health by 1. After a while, It will destroy the object and give the player more score. it is only their for you to destroy it and increase your score. if that makes sense
With your original script and description and the fact that you have attached the script to player and your recent comments do not make sense.
You need to outline exactly what you are doing. For example
You have a player
You have an object
You have a movement script.
What exactly you want done to the object (I know you just explained it a little more)
What exactly you want done to the player
As said previously, you will need at a $$anonymous$$imum a script that has to attach to the object (I don't care what you call it, I said enemy as an example but you should be as descriptive as possible).
You can first try taking your original script and ins$$anonymous$$d attach it to the object and not the player as you mentioned you were doing. That is probably a step closer to what you want.
And remove this
player = GameObject.FindWithTag("Player");
from the Update function, it only needs to be done once in start.
You need to make sure that the player GameObject is tagged properly though.
Answer by mnmwert · Sep 01, 2016 at 12:01 PM
I have a player in a scene who you can move around a shoot with, As I gain score by shooting things I will level up:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class playerMovement : MonoBehaviour
{
public float speed;
public float rotationSpeed;
public Transform Target;
public Transform target1;
public GameObject Bullet;
public float fireSpeed, fireRate,f1,f2;
public Transform Spawn;
public Transform Spawn1;
public int health;
public float healthRegene, healthres;
public int maxHealth;
public int score;
public int level;
public Text level1;
public bool duel = false;
void Start ()
{
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.W))
{
transform.position = Vector3.MoveTowards(transform.position,Target.position,speed);
}
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(-Vector3.up * rotationSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
transform.position = Vector3.MoveTowards(transform.position, target1.position, speed);
}
if (Input.GetKey(KeyCode.Mouse0) && Time.time > fireRate)
{
fireRate = Time.time + fireSpeed;
Instantiate(Bullet,Spawn.position,Spawn.rotation);
}
if (Input.GetKey(KeyCode.Mouse1) && duel == true && Time.time > f1)
{
f1 = Time.time + f2;
Instantiate(Bullet, Spawn1.position, Spawn1.rotation);
}
if(health <= 0)
{
Destroy(gameObject);
}
if (Time.time > healthRegene && health <= maxHealth)
{
healthRegene = Time.time + healthres;
health += 1;
}
level = level1.GetComponent<LevelUp>().level;
if(level >= 5)
{
fireRate = .2f;
}
if(level >= 10)
{
maxHealth = 250;
}
if(level >= 15)
{
speed = 2;
}
if(level >= 20)
{
healthRegene = .5f;
}
if(level >= 25)
{
duel = true;
}
if(level < 25)
{
duel = false;
}
if(level >= 30)
{
Bullet.GetComponent<bulletSpeed>().life = 160;
}
f1 = fireRate;
f2 = fireSpeed;
}
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "object")
{
health -= 1;
}
if(collision.gameObject.tag == "enemybullet")
{
health -= 1;
}
if(collision.gameObject.tag == "enemy")
{
health -= 1;
}
}
}
I have my block script to control the block, and a score and level scripts to control those. I want the object to be destroyed by: Time, The Player, The Players Movement. I want the Player to move around in the game. When The player does something that ensues damage, His health decreases by 1 or more (depending on the significance). If his health is 0 you go to the game over screen.
I found the problem! it had nothing to do with my scripting! my enemy was tagged player so when it was destroyed it couldn't find the player! Thanks for helping!