Non-MonoBehaviour object can access to its functions or interact with a MonoBehaviour'ed object in someway?
I have 3 CSharp files: ProyectileMono, Proyectile and Arrow.
Arrow inherits Proyectile, and ProyectileMono controls how it moves
public class ProyectileControl : MonoBehaviour{
private GameObject targetPlayer;
//Player who spawned this proyectile
public Proyectile thisProyectile; //this gets initialized inmediatly when a player spawns a proyectile.
public bool wasFacingRight;
private float direction;
void Start ()
{
Destroy (gameObject, 3); //Auto destroy it in 3 seconds if nothing was impacted.
GetComponent<SpriteRenderer> ().flipX = (wasFacingRight) ? false : true; //If the player who spawned this proyectile was facing right, then just flip the sprite.
direction = (wasFacingRight) ? thisProyectile.movementSpeed : -thisProyectile.movementSpeed; //If player was facing right or left, move this proyectile to the correct direction.
}
void Update ()
{
GetComponent<Rigidbody2D> ().velocity = new Vector2 (direction, GetComponent<Rigidbody2D> ().velocity.y); //Add velocity to the proyectile. (velocity on Y is currently unused.
}
void OnTriggerEnter2D (Collider2D collider)
{ //If proyectile hits a collider or pass through one
if (collider.tag == "Enemy") { //If collider is an enemy
collider.gameObject.GetComponent <EnemyAI1> ().Damage (thisProyectile.damage); //Damage the enemy
Destroy (gameObject); //and then destroy it.
} else if (collider.tag == "Wall") //If collider is a wall
Destroy (gameObject); //Then just destroy this proyectile
}
}
public class Proyectile
{
public float movementSpeed{ get; protected set; }
public float damage{ get; protected set; }
}
public class Arrow : Proyectile {
public Arrow(){
movementSpeed = 7;
damage = 13;
}
}
The problem is that using this, I can't interact with MonoBehaviour's methods/functions, such as GetComponent<>(), Destroy(), etc. inside of Proyectile or even in Arrow's class.
I wasn't having problems using this method, but I found that I really need some MonoBehaviour's methods like GetComponent or gameObject.AddComponent and add an animation, because I want to animate some proyectiles.
My question is this: Do I have to use a MonoBehaviour for every single thing I want to use Instead? For example, I have coded the same way my enemies, but I can use a method on its base class (Enemy class) called "DoDamage()" and execute an animation or interact with its transform or something. Do I need to create a Mono for Enemy1, Enemy2, Enemy.... for especific code for the same function "DoDamage()" and someother actions, and then a EnemyControl for just movement or common functions? OR There is actually a way to interact with objects with MonoBehaviours with out being one?