Hey, I got some Problem. I´m trying to shoot down my character. In the Script Hit Player, the Script should check, if the player ist hit. When the player is hit, the Script call the Function Hit from the script FireBall. The error is "
NullReferenceException: Object reference not set to an instance of an object
HitPlayer.Start () (at Assets/Scripts/HitPlayer.cs:25)" and "
NullReferenceException: Object reference not set to an instance of an object
HitPlayer.Update () (at Assets/Scripts/HitPlayer.cs:41)"
Need some help please and sorry for my bad english
using UnityEngine;
using System.Collections;
public class HitPlayer : MonoBehaviour
{
public int damage = 15;
private bool hit = false;
private bool hasHit = false;
private FireBall shotI;
private GameObject shot;
private Health playerHealth;
private GameObject player;
// Use this for initialization
void Start()
{
shot = GameObject.FindGameObjectWithTag("Shot");
shotI = shot.GetComponent<FireBall>();
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<Health>();
}
// Update is called once per frame
void Update()
{
if (hit == true)
{
hit = false;
shotI.Hit();
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
hit = true;
}
}
}
using UnityEngine;
using System.Collections;
public class FireBall : MonoBehaviour {
public int damage = 15;
public Transform start;
public Rigidbody shot;
public Collider Ball;
private Rigidbody shotI;
private Health playerHealth;
private GameObject player;
private float timeB = 5f;
private float timer = 0f;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<Health>();
//shotI = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
}
void OnTriggerStay(Collider other)
{
Fire();
}
void Fire()
{
if(timer >= timeB)
{
timer = 0f;
//ja = true;
shotI = Instantiate(shot, start.position, start.rotation) as Rigidbody;
shotI.velocity = transform.TransformDirection(transform.forward * 3);
//shotI.AddForce(Vector3.forward * 30);
}
}
public void Hit()
{
Debug.Log("Hier");
if (playerHealth.health > 0)
playerHealth.Damage(damage);
}
}