- Home /
C# Inheritance Question
I need some help setting up an abstract class. Im confused about class variables and how they inherit, if they even do. Spawntime is being treated as a variable shared by all subclasses. Also, is it possible for the child classes to extend on the abstract classes Update() method? Here is the Abstract Class code I have now:
public abstract class AbstractProjectile : MonoBehaviour {
public bool DestroyOnTimer = false;
public bool DestroyWhenInvisible = true;
public float TimeToLiveInSeconds = 1.0f;
//Time the Projectile was created
private float spawnTime;
// Use this for initialization
void Start ()
{
spawnTime = Time.time;
}
// Update is called once per frame
void Update ()
{
//Destroy the projectile if its timer is over
if (DestroyOnTimer) {
if ((Time.time - spawnTime) > TimeToLiveInSeconds)
{
Destroy(gameObject);
}
}
//Call Child projectiles move method
move();
}
public virtual void move() { }
}
Here is the Child Code:
using UnityEngine;
using System.Collections;
public class SinBulletScript : AbstractProjectile {
public float speed = 1000.0f;
public float Amplitude = 600.0f;
public float Omega = 15.0f;
// Use this for initialization
void Start ()
{
Vector3 shipPosition = GameObject.Find("Ship").transform.position;
shipPosition.y += 100;
transform.position = shipPosition;
}
void move()
{
gameObject.rigidbody.velocity = new Vector3(Amplitude * Mathf.Sin(Time.time * Omega), speed, 0);
}
}
Answer by Julien-Lynge · May 12, 2013 at 05:09 AM
I only see one question: "is it possible for the child classes to extend on the abstract classes Update() method?", so I'll answer that.
The answer is no. Update is a method that has special meaning to Unity - you can think of it as reserved. You can't redefine Update as abstract. Unity will call your Update function, but you can certainly define another function that gets called from Update - say myUpdate - and do whatever you want with it.
Just to point out that Unity finds Update/Awake/Start etc by scanning on the actual type which is created - you can declare any of these methods as protected in a base class and then just add a
base.Start()
Or whatever to the inherited class. You do not need to make these methods virtual or abstract for this to work (and it saves memory if you don't), but you should declare the method as new in the derived class:
protected new Start()
So I'd disagree and say you can extend upon the base classes method, but the base class doesn't need it if it's got no functionality - I think @Juts is implying that there should be code in there (rather than that being abstract) and so I'd go with being able to do that.
Answer by Thom Denick · May 12, 2013 at 05:48 AM
As for your variable question. It's confusing, but private variables are not accesible by children. You need to take a look at the protected keyword, which are variables your children will have access to. I believe this keyword is unique to C#, so if you are coming from other languages, it is probably confusing.
http://msdn.microsoft.com/en-us/library/ms173121(v=VS.80).aspx