Inheritance Question
This is the comparison I am making:
if (aiController.attackTarget == entity)
aiController.attackTarget is of type LivingEntity and entity is of type AIController. Can I compare these 2 different types because of inheritance? Can I say, an AIController is a LivingEntity.
This is the setup I am using:
IEntity (interface)
using UnityEngine;
public interface IEntity
{
Vector3 position { get; set; }
GameObject gameObject { get; }
}
Entity.cs (base class)
using UnityEngine;
public abstract class Entity : MonoBehaviour, IEntity
{
public Vector3 position
{
get { return this.transform.position; }
set { this.transform.position = value; }
}
}
LivingEntity.cs
using UnityEngine;
public abstract class LivingEntity : Entity
{
private LivingEntity _attackTarget;
public LivingEntity attackTarget
{
get { return _attackTarget; }
set { _attackTarget = value; }
}
}
AIController.cs
using UnityEngine;
public class AIController : LivingEntity
{
// Stuff
}
Answer by daleth90 · Jun 08, 2016 at 05:40 AM
Yes, you can just use == because == is the same as the ReferenceEquals().
It just checks whether they are the same instance.
In contrast, Equals() checks value equality.
By the way, since you've ALREADY CHOSE to use inheritance, so "an AIController IS a LivingEntity".
Thanks for the explanation. This is the first time I have ever actually used inheritance.
Actually, I want to say that "HAS-a" should be better in this case. You can google "composition vs inheritance" to read more.
However, It's just the first time you use inheritance, so don't worried about that now.