The enemy.cs is crazy! [SOLVED]

UnAssignedReferenceException…

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

public class enemy : MonoBehaviour
{
[SerializeField] int Health = 100;
[SerializeField] float shotCounter;
[SerializeField] float minTimeBetweenShots = 0.2f;
[SerializeField] float maxTimeBetweenShots = 1f;
[SerializeField] GameObject Projectile;
[SerializeField] float ProjectileSpeed = 10f;

// Start is called before the first frame update
void Start()
{
    shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
}

// Update is called once per frame
void Update()
{
    CountDownAndShoot();
}

private void CountDownAndShoot()
{
    shotCounter -= Time.deltaTime;
    if (shotCounter <= 0f)
    {
        Fire();
        shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
    }
}


private void Fire()
{
    GameObject laser = Instantiate(Projectile, transform.position, Quaternion.identity) as GameObject; the error pointed this.....
    laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -ProjectileSpeed);
}

private void OnTriggerEnter2D(Collider2D other)
{
    DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
    Health -= damageDealer.GetDamage();
    if (Health <= 0)
    {
        Destroy(gameObject);
    }
}

}

In laser defender…
red laser has:
DamageDealer.cs
RigidBody
And Capsule Collider

The enemy has:
Enemy.cs
and Box collider

the error said 9999999999999999+:

The variable Projectile Has not been assigned
need assign projectile of enemy in the inspector.

I ASSIGNED THE PROJECTILE INTHE INSPECTOR

pls help me…

Hi,

Could you please share screenshots of what you see in Unity?

Double click on the error message. To which line in your code does it refer?

Is the projectile assigned to the prefab? Check all your enemies in the scene.

0_0 Thank you!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.