Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Folcon · Jan 26, 2010 at 01:16 AM · getcomponentinheritance

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Brian-Kehrer · Jan 26, 2010 at 02:50 AM 0
Share

Retagged with inheritance and c#specific, since this is relevant to users of both languages because of language choice.

avatar image runevision ♦♦ · Feb 17, 2010 at 12:09 PM 1
Share

I removed both javascriptspecific and c#specific tags since the question and answers are relevant regardless of language choice.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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....

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Folcon · Jan 26, 2010 at 01:55 AM 0
Share

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?

avatar image Brian-Kehrer · Jan 26, 2010 at 01:58 AM 0
Share

Yes, these calls will still trigger as long as the functions are specified on OnDeath, I'll post an example...

avatar image Folcon · Jan 26, 2010 at 02:12 AM 0
Share

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 :).

avatar image Brian-Kehrer · Jan 26, 2010 at 02:17 AM 0
Share

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.

avatar image Brian-Kehrer · Jan 26, 2010 at 02:20 AM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Inheritance vs RequireComponent -1 Answers

How can I access an inherited method from a separate (collided) object? 1 Answer

C# - How do you GetComponent of a child class that inherits from a specific class? 1 Answer

Inheritance and using GetComponent 2 Answers

Using GetComponent with a subclass 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges