- Home /
Organise scripts with the same name?
Say I have a generic health script and that links into a script called onDeath when you die. Now I have several versions of onDeath depending on what happens if you die. So each character will only have one death script and according to what it says, that's how they die.
The problem is I can't seem to have more than one script with the same name and I don't necessary know which death script I'm calling before hand, it depends on what character dies. This is also good I think for organising my code into separate components. Is there any other way I can keep things like the different ways to die separate and then hook them on when the character initialises?
Any suggestions or alternate methods of implementing what I'm talking about in case I have completely the wrong approach?
Retagged with inheritance and c#specific, since this is relevant to users of both languages because of language choice.
I removed both javascriptspecific and c#specific tags since the question and answers are relevant regardless of language choice.
Answer by Brian-Kehrer · Jan 26, 2010 at 01:46 AM
Extend new classes from OnDeath (or whatever the class name)
in Javascript, I believe its
class PlayerDeath extends OnDeath{
} class EnemyDeath extends OnDeath{
}
... or in C#
public class PlayerDeath : OnDeath{
}
If you are using C# (and maybe Javascript, though I haven't tried) you can also use interfaces to accomplish a similar result. Read more about interfaces here
The beauty of this is your health script can simply reference all the various scripts as type OnDeath. Make sure to save each one in its own file with the same name as the class.
Specify any functions you want accessible to all subclasses in OnDeath, and then override them in the subclasses, if necessary.
EDIT Further details below: Sorry, I'm switching to C#, it's far easier to do / find examples for more advanced coding.
using UnityEngine; using System.Collections;
public abstract class OnDeath : MonoBehaviour {
public abstract void Die();
}
Above is an example abstract class in C#. What's nice about this, it that it forces all scripts inheriting from it to implement the Die function, that way you can be sure it is always possible to call Die().
Below is the subclass:
using UnityEngine; using System.Collections;
public class EnemyDeath : OnDeath{
public override void Die(){
Debug.Log("I'm a subclass");
}
}
And below the test class. Attach both this and EnemyDeath to the same GameObject, and woila! Notice we are casting to type OnDeath, not enemy death... You can treat the object as an OnDeath as well.
using UnityEngine; using System.Collections;
public class TestScript : MonoBehaviour {
void Start(){
((OnDeath)GetComponent(typeof(OnDeath))).Die();
}
}
If readers must use Javascript, it can sort of be done. C# is far superior for good OO though. Note that commenting out Die() on EnemyDeath does not trigger an error, as it does with overriding an abstract class in C#.
class OnDeath extends MonoBehaviour {
virtual function Die(){
}
}
class EnemyDeath extends OnDeath{
function Die(){
Debug.Log("blah");
}
}
It was asked about a c# reference - Accelerated C# 2008 is the one I used to learn C# while meddling in Unity. It's a great book for the new C# programmer who has some basic knowledge about programming, or has programmed in other languages. One day someone should write one specific to Unity....
How do I call the script externally? Aren't names referenced? So for example I call onDeath.Die(), and that game object has a EnemyDeath on them, will the Die trigger on the enemy? What about calls to Send$$anonymous$$essage? I'm thinking of using direct calls when possible but things like Send$$anonymous$$essage for events. Will that still work with inheritance?
Yes, these calls will still trigger as long as the functions are specified on OnDeath, I'll post an example...
Thanks, $$anonymous$$. Just getting my head around the way unity does things :). I'm assu$$anonymous$$g this behaviour is C# specific? I've been sticking to unityscript for now(it doesn't feel like javascript =) ) and I've not seen any kind of interfaces with regards to it, some stuff on inheritance though :).
It's not C# specific, there are ways to do all of these things in Javascript. However the syntax is far easier to understand in C#, as you can kindof see what is going on. In Javascript it is all quite hidden, and I think that creates confusion with respect to object oriented program$$anonymous$$g.
That said, I can't seem to mark anything as 'Abstract' in Unityscript, so while you can still do inheritance, it isn't nearly as clean
Your answer
![](https://koobas.hobune.stream/wayback/20220612031714im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I reference a generic BaseClass using GetComponentInParent<>() 2 Answers
Is it possible to set variables for classes? 2 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer
When or where do I set a prefab script subclass property? 2 Answers
Inheritance vs RequireComponent -1 Answers