Inherance script problem [C#]
Hi everybody ! I have a problem with base script : i want to use it for differents types of turrets but i don't know how to solve problems : Instantiate, transform, startCoroutine and StopCoroutine are not recognized...
This is my two scripts :
BaseTower :
using UnityEngine;
using System.Collections;
using System;
public class BaseTower
{
private string towerName;
private string towerDescription;
private int towerPrice;
private float towerDamage;
private int towerRange;
public string TowerName
{
get { return towerName; }
set { towerName = value; }
}
public string TowerDescription
{
get { return towerDescription; }
set { towerDescription = value; }
}
public int TowerPrice
{
get { return towerPrice; }
set { towerPrice = value; }
}
public float TowerDamage
{
get { return towerDamage; }
set { towerDamage = value; }
}
public int TowerRange
{
get { return towerRange; }
set { towerRange = value; }
}
private GameObject mob;
public GameObject bullet;
public GameObject gun;
public int power = 100;
private bool isFiring;
private IEnumerator fireRoutine;
void Update()
{
if (mob != null)
{
transform.LookAt(mob.transform.position);
}
}
void OnTriggerEnter(Collider col)
{
if (col.tag == "MobCol")
{
mob = col.gameObject;
fireRoutine = FireRoutine();
StartCoroutine(fireRoutine);
}
}
void OnTriggerExit(Collider col)
{
// Make sure our initial mob should be removed
if (col.gameObject == mob)
{
if (fireRoutine != null)
{
StopCoroutine(fireRoutine);
}
mob = null;
}
}
public IEnumerator FireRoutine()
{
// Use a Coroutine for this
WaitForSeconds wait = new WaitForSeconds(0.2f);
// Keep ourselves from firing at nothing
while (mob != null)
{
Fire();
// Return our yield instance
yield return wait;
}
}
public void Fire()
{
GameObject towerBullet = (GameObject)Instantiate(bullet, gun.transform.position, Quaternion.identity);
towerBullet.GetComponent<Rigidbody>().AddForce(transform.forward * power);
}
}
and my tower script (not finished yet because i can't fix the first problem ) :
using UnityEngine;
using System.Collections;
public class RifleTower : BaseTower {
public RifleTower()
{
TowerName = "Rifle tower";
TowerDescription = "Fast fire rate";
TowerPrice = 100;
TowerRange = 5;
TowerDamage = 1f;
}
}
Thank you ! bye, xyHeat
class should derive from $$anonymous$$onoBehaviour for transform and coroutine to work (and lots of other things)
public class BaseTower : $$anonymous$$onoBehaviour
or you can make an IBaseTower interface or abstract class for the fields
then your BaseTower class should look like public class BaseTower : $$anonymous$$onoBehaviour, IBaseTower
If i do that, can i use my second script ? ( with : public class RifleTower : BaseTower )
It should work fine with the first method fine. (Just add $$anonymous$$onoBehaviour)
Second one looks better I$$anonymous$$O.
Your answer
Follow this Question
Related Questions
How can i access another scripts GameObject Array to check its inside if there are any tower 1 Answer
How to use value from a function in another function 1 Answer
Creating a single class or use polymorphism? 1 Answer
GameObject.Find : why not working? 3 Answers
Fading in Smoothly between Sprites 0 Answers