- Home /
Question by
wysepkawyspa · May 22, 2019 at 08:30 AM ·
inheritancespherecolliderclass instance
C# Inheritance and passing Components
Hi !,
Been learning inheritance and passing variables and parameters between classes and subclasses. Also tried to setup sphere collider from base class by passing neccesary variables from derived class, but what is happening that collider is set up on desired gameObject and then another instance of sphereCollider is setUp in the middle of the scene.
public class RobotAI : MonoBehaviour
{
int robotsLayer = (1 << 9);
int maxHeath = 100;
int currentHealth;
protected NavMeshAgent agent;
protected SphereCollider sphereCollider;
protected RaycastHit[] rayEnemyCol;
protected string enemyNameOrTag;
public virtual void Start()
{
}
public virtual void SetupGameObject(float colRadius , string enemyTag , float agentSpeed)
{
sphereCollider = gameObject.AddComponent<SphereCollider>();
sphereCollider.radius = colRadius;
sphereCollider.isTrigger = true;
agent = gameObject.GetComponent<NavMeshAgent>();
agent.speed = agentSpeed;
enemyNameOrTag = enemyTag;
currentHealth = maxHeath;
}
// Update is called once per frame
public virtual void Update()
{
CheckForEnemy();
}
And subclass:
public class Cyborg : RobotAI
{
float colRadius = 4f;
string enemyTag = "Zoorg";
float agentSpeed = 2.5f;
float angleView = 50f;
public override void Start()
{
SetupGameObject(colRadius , enemyTag , agentSpeed);
}
private void OnDrawGizmos()
{
if (Application.isPlaying)
{
Gizmos.DrawWireSphere(sphereCollider.center, colRadius);
}
}
So if anybody got any slightest idea of why this is specifically happening and why im not refering to sphereCollider on gameObject itself, would appreciate any critic or help. Cheers !
unityhalp.png
(390.2 kB)
Comment
Your answer
