Přidat otázku mezi oblíbenéZasílat nové odpovědi e-mailemVyřešeno Unity 3D a C#

Hraju si s unity 3D, mám jednoduchou plošinovku s charakterem, šipkama se běhá, skáče, mezerníkem seká do nepřátel. Vtip je v tom, že to funguje, ovšem jen někdy :D

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAttack : MonoBehaviour {

    private float timeBtwAttack;
    public float startTimeBtwAttack;

    public Transform attackPos;
    public LayerMask whatIsEnemies;
    public float attackRange;
    public int damage;

    private Shake shake;

    public GameObject bloodEffect;

    void Start (){
        shake = GameObject.FindGameObjectWithTag("ScreenShake").GetComponent<Shake>();
    }

    void Update(){

        if(timeBtwAttack <=0){
            // then you can attack
            if(Input.GetKey(KeyCode.Space)){
                Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
                for (int i = 0; i < enemiesToDamage.Length; i++) {
                    enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage);
                    /*shake cam a animace utoku*/ 
                    shake.CamShake();
                    Instantiate(bloodEffect, transform.position, Quaternion.identity);
                    Debug.Log("Cakance krve!!!!!!");                 
                }
            }
        if(Input.GetKey(KeyCode.Space)){
            timeBtwAttack = startTimeBtwAttack;        
        } else {
            timeBtwAttack -= Time.deltaTime;
        }
    }

    }
    void OnDrawGizmosSelected()
        {
            Gizmos.color = Color.red;
            Gizmos.DrawWireSphere(attackPos.position, attackRange);
        }
}

a enemy je


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour {    

    public int health;
    public float speed;
    private float dazedTime;
    public float startDazedTime;

    private Animator anim;
    /*public GameObject bloodEffect;*/

    void Start(){
        //anim = GetComponent<Animator>();
        //anim.SetBool("isRunning", true);
    }
    
    void Update(){

        if(dazedTime <= 0){
            speed = speed;
        } else {
            speed = 0;
            dazedTime -=Time.deltaTime;
        }

        if(health <= 0){
            Destroy(gameObject);
        }

        transform.Translate(Vector2.left * speed * Time.deltaTime);
    }

    public void TakeDamage(int damage){
        // play a hurt sound
       /* Instantiate(bloodEffect, transform.position, Quaternion.identity);*/ 
        health -= damage;
        Debug.Log("damage TAKEN");
    }
}

Jak říkám, v podstatě to nějak funguje, ale občas je nepříteli udělen dmg 1-6 jindy nula. Vůbec nechápu kde se bere ta náhodnost.
Dostávám chybovou hlášku :

NullReferenceException: Object reference not set to an instance of an object
PlayerAttack.Update () (at Assets/Scripts/PlayerAttack.cs:30)

což by mělo být konkrétně

  enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage);

Ale netuším co je na tom špatně.

Rád bych poprosil o pomoc, věřím, že někomu kdo umí programovat to bude jasné, ale hraju si s tím teprve pár dní, tak prosím o shovívavost:)

Odpověď na otázku

1 Zadajte svou přezdívku:
2 Napište svou odpověď:
3 Pokud chcete dostat ban, zadejte libovolný text:

Zpět do poradny