0

I have a main class that looks like this:

public abstract class Soldier : Monobehaviour
{
public virtual T GetClosestEnemy<T>(T soldierType) where T : MonoBehaviour
{
    T[] soldiers;
    soldiers = FindObjectsOfType<T>(soldierType);
    T closestSoldier = null;
    float dist = Mathf.Infinity;
    Vector3 thisPos = transform.position;
    foreach(T soldier in soldiers)
    {
        print("looping");
        Vector3 difference = soldier.transform.position - thisPos;
        float currentDist = difference.sqrMagnitude;
        if (currentDist < dist)
        {
            print("found it");
            closestSoldier = soldier;
            dist = currentDist;
        }
    }
    print(closestSoldier.gameObject.name);
    return closestSoldier;
}
}

And here is the derived class:

public class GoodSoldier : Soldier
{
    public BadSoldier closestBadSoldier = null;

public override BadSoldier GetClosestEnemy<BadSoldier>(BadSoldier soldierType)
{
    
    return base.GetClosestEnemy(soldierType);
}
void Update()
{
     GetClosestEnemy<BadSoldier>(closestBadSoldier);
    
}
}

The base method works just fine. I reference it in the subclass and it also works just fine. It prints the right object's name. But closestBadSoldier is still null. How can that even be possible? I can't reference it in any other methods or it gives me a Null Reference Exception.

Deniz Demir
  • 223
  • 1
  • 2
  • 8
  • I don't see any code where you assign anything to `closestBadSoldier`, so it'll stay at the initialized value of `null`. Do you set it somewhere else somehow? – John Wu Nov 19 '21 at 21:54
  • @JohnWu i thought "return closestSoldier;" line would assign closestBadSoldier since I put it in as parameter. I can't for the life of me figure out how I will assign it to closestBadSoldier. – Deniz Demir Nov 19 '21 at 22:15
  • You `Update` method doesn't do anything with the result of the `GetClosestEnemy` – Jeroen van Langen Nov 19 '21 at 22:21
  • @JeroenvanLangen yes, I've been at it for several hours now, maybe it's the sleep deprevation but I can't figure out how to do anything with the result. I thought if I just put `closestBadSoldier` as a parameter to `GetClosestEnemy` , it would be assigned and that would be it. But now that it hasn't, I'm lost. – Deniz Demir Nov 19 '21 at 22:49
  • @DenizDemir What do you want to do with the closest enemy? – Jeroen van Langen Nov 19 '21 at 23:12

1 Answers1

0

Just update your overridden method to set the value to the variable

public override BadSoldier GetClosestEnemy<BadSoldier>(BadSoldier soldierType)
{
    closestBadSoldier = base.GetClosestEnemy(soldierType);
    return closestBadSoldier;
}
Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106
  • thanks for answering Mr. Bitar, this line closestBadSoldier = base.GetClosestEnemy(soldierType); gives a Compiler Error CS0029 – Deniz Demir Nov 19 '21 at 22:13
  • @DenizDemir You need to do explicit conversion to resolve this error `closestBadSoldier = base.GetClosestEnemy(soldierType) as BadSoldier`; – Burhan Savci Nov 20 '21 at 08:06
  • @BurhanSavci thank you Burhan, i ended up using pass by reference which did the trick perfectly. :) – Deniz Demir Nov 21 '21 at 15:29