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);
}
}
}
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.