C# Inheritance Issues

Hey guys,

would love some help with this issue.

I have my base properties class which contains bot properties.

using UnityEngine;
using System.Collections;

// Bot Properties
public class Bot : MonoBehaviour {
	
	public float health;				// Health of bot
	public float moveSpeed; 			// Moving speed of bot
	public float turnSpeed; 			// Turning speed of bot
	public float AmmoCrateDropRate;	// The bots chance to drop ammo crate on death 
	public AmmoData [] ammoDrops; 	// List of possible ammo drops
	
}

Below is the controller class for a basic bot.

using UnityEngine;
using System.Collections;

// Handles Bot behaviour
public class BotController : MonoBehaviour {
	
	public Bot bot; // Bot details

	// State "DYING"
	// Has taken damage equal or greater then its health
	// and has been killed
	protected override void DyingState() {
	
                // Do death animation
		// Destroy self
		
	}

...

}

Below is my derived properties class

using UnityEngine;
using System.Collections;

public class Bot_ZombieExplodingGrunt : Bot {

	public float explosionDamage;
	public float explosionRadius;
	
}

// Below is my derived bot controller class

using UnityEngine;
using System.Collections;

public class BotController_ZombieExplodingGrunt : BotController {
	
	public new Bot_ZombieExplodingGrunt bot;
	
	// State "DYING"
	// Has taken damage equal or greater then its health
	// and has been killed
	protected override void DyingState() {
	
		// Do explosion on death
	        Physics.OverlapSphere(obj.position, bot.explosionRadius);

		base.DyingState();
		
	}
	
}

Most bots will run fine on the base class, but I want a few to have additional behaviour. The code compiles fine in monodeveloper, but in unity it gives me this error.
Same name multiple times included:::Base(MonoBehaviour) bot

The issue is that my bot now has two bot fields showing up in the inspector.

caused by this: public new Bot_ZombieExplodingGrunt bot;

the issue remains with and without the “new”

How is the correct way of doing inheritance in c#?

Help would be appreciated, I have spent hours trying different things, searching through C# examples and unity forums.

I know I could properly just do “public Bot_ZombieExplodingGrunt explodingBot” or something in the derived controller class, but I was hoping for a neater method.

Thanks!

Edit: I also tried to use casting, but it didn’t seem to work.

Edit: Also this is for iOS devices.

You already have a variable named ‘bot’. You seem to want to change the type of the ‘bot’ variable in your child class, but that is not possible. Remove the public Bot_ZombieExplodingGrunt line and use the bot of the parent class. Yes, this will mean you will need to cast bot to (Bot_ZombieExplodingGrunt) whenever you want to use Bot_ZombieExplodingGrunt functions and variables, but that’s how it works.

Microsoft C# allows you to do exactly what Pip wants: http://msdn.microsoft.com/en-us/library/ms173152(VS.80).aspx

I am not sure about Mono, I’ve tried to find something relevant on polymorphism with no success.

The problem is that Unity is using reflection to iterate through your class. The new keyword isn’t quite the same as an override, it only hides your old definition from objects referencing your derived class; the old value is still there, and is accessible through reflection among other ways.

If you want to hide it from Unity, perhaps you can try marking your base class’ old field as [Nonserialized]: Unity - Scripting API: NonSerialized

Edit: I forgot you also want your base class to be inspectable, in which case I’m not sure how to do exactly what you want :-S

Thanks for the feedback.

tomvds, your suggestion was what I originally tried to do. Unfortunately casting didn’t seem to work either.

(Bot_ZombieExplodingGrunt)bot.explosionRadius

Actually I retried casting, I am not sure what I did the first time around but it worked this time.

Bot_ZombieExplodingGrunt explodingBot = (Bot_ZombieExplodingGrunt)bot;

Unfortunately its not as neat(dynamic) as I was hoping it to be, but it works.

A big thanks you all of you for your time and assistance.

Could the Bot class not contain a PerformDeath function. That way the controller can remain generic, and can still control how the bot moves, but you just need to override the PerformDeath function inside of the ExplodingGrunt class to implement the new behaviour.

i.e.

using UnityEngine;
using System.Collections;

// Bot Properties
public class Bot : MonoBehaviour {
	
	public float health;				// Health of bot
	public float moveSpeed; 			// Moving speed of bot
	public float turnSpeed; 			// Turning speed of bot
	public float AmmoCrateDropRate;	// The bots chance to drop ammo crate on death 
	public AmmoData [] ammoDrops; 	// List of possible ammo drops

public virtual void PerformDeath()
{

}
}

using UnityEngine;
using System.Collections;

public class Bot_ZombieExplodingGrunt : Bot {

	public float explosionDamage;
	public float explosionRadius;

public override void PerformDeath()
{
Physics.OverlapSphere(obj.position, bot.explosionRadius);
}
	
}

using UnityEngine;
using System.Collections;

// Handles Bot behaviour
public class BotController : MonoBehaviour {
	
	public Bot bot; // Bot details

	// State "DYING"
	// Has taken damage equal or greater then its health
	// and has been killed
	protected override void DyingState() {
	
                bot.PerformDeath();
		
	}

...

}

This would allow all bots to die, and a controller to decide when to die, but to more easily alter how each individual bot dies.

Sorry I am not sure I understand, my current design is.

                        Bot Manager (Handles bots as a collective)
                                          |
                                          |
                                         \ /
Bot Controller (Individual Behaviour) <------> Bot (Properties of bot)

That cast the result of bot.explosionRadius to Bot_ZombieExplodingGrunt, whereas what you need to do is cast bot to Bot_ZombieExplodingGrunt then fetch explosionRadius:

((Bot_ZombieExplodingGrunt)bot).explosionRadius

It’s an operator precedence thing :slight_smile:

Thanks!