'Enemy' does not contain a definition for 'RewardGold'

Hello i am facing an error with my script.

‘Enemy’ does not contain a definition for ‘RewardGold’ and no accessible extension method ‘RewardGold’ accepting a first argument of type ‘Enemy’ and Enemy’ does not contain a definition for ‘StealGold’ and no accessible extension method ‘StealGold’ accepting a first argument of type ‘Enemy’ could be found.

i think the problem might be connected to the fact my EnemyMover script is called enemy and my Enemy script is called Enemy2, heres. my script i can send more information if needed.

enemy script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
[SerializeField] List path = new List();
[SerializeField] [Range(1f, 5f)] float speed = 1f;

Enemy enemy;
void OnEnable()
{
    FindPath();
    ReturnToStart();
    StartCoroutine(FollowPath());
}
void Start()
{
    enemy = GetComponent<Enemy>();
}

void FindPath()
{
    
    path.Clear();

    GameObject[] waypoints = GameObject.FindGameObjectsWithTag("Path");

    foreach (GameObject waypoint in waypoints)
    {
        path.Add(waypoint.GetComponent<Waypoint>());
    }
}

void ReturnToStart()
{
    transform.position = path[0].transform.position;
}

IEnumerator FollowPath()
{
    foreach (Waypoint waypoint in path)
    {
        // defining variables
        Vector3 startPos = transform.position;
        Vector3 endPos = waypoint.transform.position;
        float travelPer = 0f;

        transform.LookAt(endPos);
    // while loop
        while (travelPer < 1f)
        {
            travelPer += Time.deltaTime * speed;
            transform.position = Vector3.Lerp(startPos, endPos, travelPer);
            yield return new WaitForEndOfFrame();
        }
    }
    gameObject.SetActive(false);
    enemy.StealGold();
}

}

enemy2 script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy2 : MonoBehaviour
{
[SerializeField] int goldReward = 25;
[SerializeField] int goldPenalty = 25;

Bank bank;
// Start is called before the first frame update
void Start()
{
    bank = FindObjectOfType<Bank>();
}

void RewardGold()
{
    if(bank = null) { return; }
    bank.Deposit(goldReward);
}
void StealGold()
{
    if(bank = null) { return; }
    bank.WithDrawl(goldPenalty);
}

}

There are two problems.

The first is that you’re trying to call methods which don’t exist in Enemy, they exist in Enemy2. You have a variable of type Enemy which means it’s any game object which has the Enemy script attached to it. What you need is a variable of type Enemy2 and in Start() have enemy = GetComponent<Enemy2>(). As it is now you’re getting the component from itself, which is redundant.

The second is that the methods you want to call (in enemy2) aren’t public, so they aren’t accessible outside of the script they’re in.

It would definitely be helpful to name your scripts to make it clear what they do. It doesn’t affect whether the code will execute properly, but it affects your ability to understand what the code is doing, especially if you come back to it later and it’s generally good practice to make everything as readable as possible.

Thank you so much for helping me out!

I will dedicated to my my scripts more clear!

1 Like

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

Privacy & Terms