Multiple errors

Please don’t kill me, i’m newbie…

So, i’m lost what should be with what, and this way i made 3 errors (if i know my actual skills, they are stupid). Don’t mind if you don’t know few words, i’m just mixing english and my own language (polish)

  1. at Karabin 108,21: error CS1061: ‘BasicEnemy’ does not contain a definition for ‘takeDamage’ and no accessible extension method ‘takeDamage’ accepting a first argument of type ‘BasicEnemy’ could be found (are you missing a using directive or an assembly reference?)

2 and 3 at BasicEnemy 54,3 and 22: error CS0103: The name ‘basicEnemy’ does not exist in the current context

and codes:

  1. BasicEnemy
using System.Diagnostics;
using System.Security.Cryptography;
using UnityEngine;

public class BasicEnemy : MonoBehaviour {

    [HideInInspector]
    public float speed;
    public float startSpeed = 10f;

    public int health = 150;

    public int moneyGain = 30;

    public GameObject deathEffect;

    private Transform target;
    private int wavepointIndex = 0;

    void Start()
    {
        speed = startSpeed;
        target = Waypoints.points[0];
    }

    public void TakeDamage (int amount)
    {
        health -= amount;
        if (health <= 0)
        {
            Die();
        }
    }

    void Die ()
    {
        GameObject effect = (GameObject)Instantiate(deathEffect, transform.position, Quaternion.identity);
        Destroy(effect, 2f);

        PlayerStats.Money += moneyGain;
        Destroy(gameObject);
    }

    void Update()
    {
        Vector3 dir = target.position - transform.position;
        transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);

        if (Vector3.Distance(transform.position, target.position) <= 0.4f)
        {
            GetNextWaypoint();
        }

        basicEnemy.speed = basicEnemy.startSpeed;
    }

    public void Slow(float pct)
    {
        speed = startSpeed * (1f - pct);
        speed = startSpeed;
    }
    void GetNextWaypoint()
    {
        if (wavepointIndex >= Waypoints.points.Length - 1)
        {
            EndPath();
            return;
           
        }

        wavepointIndex++;
        target = Waypoints.points[wavepointIndex];
    }

    void EndPath ()
    {
        PlayerStats.Lives--;
        Destroy(gameObject);
    }
}

and Karabin

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Security.Cryptography;
using UnityEngine;

public class Karabin : MonoBehaviour
{

    private Transform target;
    public float range = 20f;

    public float fireRate = 0.5f;
    private float fireCountdown = 0f;

    [Header("Rotacja, cele")]

    public Transform partToRotate;
    public float turnSpeed = 1f;

    public string enemyTag = "Enemy";

    public GameObject bulletPrefab;
    public Transform firePoint;

    [Header("Slow")]
    public bool useSlow = false;
    public float slowStrenght = .3f;

    [Header("Poison")]
    public bool usePoison = false;
    public int poisonValue = 1;

    private BasicEnemy targetEnemy;



    void Start()
    {
        InvokeRepeating("UpdateTarget", 0f, 0.3f);
    }

    void UpdateTarget ()
    {
        GameObject [] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
        float shortestDistance = Mathf.Infinity;
        GameObject nearestEnemy = null;
        foreach (GameObject enemy in enemies)
        {
            float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
            if (distanceToEnemy < shortestDistance)
            {
                shortestDistance = distanceToEnemy;
                nearestEnemy = enemy;
            }
        }

        if (nearestEnemy != null && shortestDistance <= range)
        {
            target = nearestEnemy.transform;
            targetEnemy = nearestEnemy.GetComponent<BasicEnemy>();
        } else
        {
            target = null;
        }
    }

    void Update()
    {
        if (target == null)
            return;

        LockOnTarget();

        if (usePoison)
        {
            Poison();
        } else
        {
            if (fireCountdown <= 0f)
            {
                Shoot();
                fireCountdown = 1f / fireRate;
            }
            fireCountdown -= Time.deltaTime;

            if (useSlow)
            {
                Slow();
            }
        }


    }

    void LockOnTarget ()
    {
        Vector3 dir = target.position - transform.position;
        Quaternion lookRotation = Quaternion.LookRotation(dir);
        Vector3 rotation = Quaternion.Lerp(partToRotate.rotation, lookRotation, Time.deltaTime * turnSpeed).eulerAngles;
        partToRotate.rotation = Quaternion.Euler(0f, rotation.y, 0f);
    }

    void Poison ()
    {
        targetEnemy.takeDamage(poisonValue * Time.deltaTime);
    }

    void Slow ()
    {
        targetEnemy.Slow(slowStrenght);
    }

    void Shoot ()
    {
        GameObject bulletGO = (GameObject)Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
        Pocisk bullet = bulletGO.GetComponent<Pocisk>();

        if (bullet != null)
            bullet.Seek(target);
    }

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

}

Line 108 error is just incorrect capitalization of the letter. TakeDamage, not takeDamage.

The other errors, it depends on your intentions but if you’re just trying to refer to this script’s own variables you don’t need anything in front of the variable names. Instead of “basicEnemy.speed” just write “speed”

changing takeDamage to TakeDamage gaved another error at Karabin 108,32 : Argument 1: cannot convert to float to int, other 2 are fine

You need to either cast the float to an int or round it with Mathf.RoundToInt

If i think correctly, line 108 should look like this?
targetEnemy.TakeDamage((Mathf.RoundToInt)poisonValue * Time.deltaTime);

poisonValue was already Int

The problem is your using Time.deltaTime on the poison so would need a float. If you want to iterate the poison as an integer over time then you should add Time.deltaTime to a float timer variable and after reaching >= duration poisonValue++ and reset timer to -= duration.

When you multiply int * float, C# converts the int to a float and you get a float result. Try this:

targetEnemy.TakeDamage(Mathf.RoundToInt(poisonValue * Time.deltaTime));

I tried doing timer on freezing, but it was big fail… I don’t know how i can do that

Error is gone, now some testing and i hope everything is now fine, thanks.

Or better yet, make poisonValue a float

1 Like

Ok, i see i failed even harder… i just need more sleep. Ok, skip it, i wrote freeze and poison for turret, but it can’t and shouldn’t apply I moved everything into bullet code and now i need help again… If i want to apply poison or slow, what do i need

Here is code for bullet

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;

public class Pocisk : MonoBehaviour
{
    private Transform target;

    public float speed = 70f;
    public float explosionRadius = 0f;

    public int damage = 85;

    public GameObject impactEffect;

    public bool usingPoison;
    public float poisonValue = 0;

    public bool usingSlow;
    public float slowValue = .0f;

    public void Seek (Transform _target)
    {
        target = _target;
    }

    void Update() {
        if (target == null)
        {
            Destroy(gameObject);
            return;
        }

        Vector3 dir = target.position - transform.position;
        float distanceThisFrame = speed * Time.deltaTime;

        if (dir.magnitude <= distanceThisFrame)
        {
            HitTarget();
            return;
        }

        transform.Translate(dir.normalized * distanceThisFrame, Space.World);
        transform.LookAt(target);

    }
    void HitTarget()
    {
        GameObject effectIns = (GameObject)Instantiate(impactEffect, transform.position, transform.rotation);
        Destroy(effectIns, 2f);

        if (explosionRadius > 0f)
        {
            Explode();
        } else
        {
            Damage(target);
        }
        Destroy(gameObject);

    }

    void Explode ()
    {
        Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius);
        foreach (Collider collider in colliders)
        {
            if (collider.tag == "Enemy")
            {
                Damage(collider.transform);
            }
        }
    }

    void Damage (Transform enemy)
    {
        BasicEnemy e = enemy.GetComponent<BasicEnemy>();
        if (e != null)
        {
            e.TakeDamage(damage);
        }

        if (usingSlow)
        {
            speed = startSpeed * (1f - slowStrenght);
        }

        if (usingPoison)
        {
            targetEnemy.TakeDamage(Mathf.RoundToInt(poisonStrenght * Time.deltaTime));
        }

    }
}

And there is high chance i am sleeping right now, i should be back in a few hours…