Scripting Problems

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 :smile:

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

    }


}

From what I understand about that exception, and from the line numbers you mentioned. It would seem like you don’t actually have an object of Fireball being set up properly. have you got the tags correct?

Maybe forgotten to add the script to your game object?

I have an object Fireball with the right tag. Maybe the problem ist, that I use Instantiate? Because this is spawning the Fireball.

Hmm I’m probably not the best person overall to help you, still new but every time I have had your error, it’s been because I’ve miss-spelled the tag/name of the object or because I have forgotten to add a component to said object.

I mean, I use instantiate and it works fine. Given a prefab, a position and rotation. It’s saying your error is specifically related to shotI, which is the component so I doubt it’s anything to do with instantiate personally.

I believe it’s “shot” which is missing, actually. It doesn’t exist when you call GetComponent on it (causing the first error), and as result the component doesn’t exist either (the second error).

So - is there an object tagged “Shot” in your scene?

Not at the start. When I´m in the area than the script calls

void OnTriggerStay(Collider other)
    {
        Fire();
    
    }

and than

shotI = Instantiate(shot, start.position, start.rotation) as Rigidbody;

And here comes the first object with the tag shot. Shot is a Sphere prefab with the right tag.

I meant in the HitPlayer class, actually; that’s the one where the error is occurring.
Shot doesn’t exist until the “Fire” method of Fireball, but it’s being referenced in the Start method of HitPlayer, so if HitPlayer is already in existence before Fire happens, then this code won’t work.

Either HitPlayer shouldn’t exist until the shot is fired, or else the search for shot should happen when the hit happens, not when HitPlayer is created.

If HitPlayer is a component on the shot itself, I’m not sure that you can find an object by tag while it’s in the process of being created; moving the check to when it hits may work better.