Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by Tyler 2 · Dec 03, 2010 at 11:24 PM · overridedeactivatecancel

Override a function?

Hello, is there a way to override a function in a script using another script (as in just canceling the first script's function)? Thanks

Comment
Add comment
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

6 Replies

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

Answer by diabloroxx · Dec 04, 2010 at 12:52 AM

Can't you have a boolean variable attached in the first function and then make it false using the second function. Eg:

Script A -

bool AmActive = true;

void SomeFunction() { if(AmActive) .... blah blah blah }

Then in another script, you can make the bool AmActive as false.

Script B -

ScriptA.AmActive = false;
function SimilarFunction()
{
....
}

Of course you can use this to access other gameobjects. And the scripts using the GetComponent

Comment
Add comment · 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
4

Answer by _Petroz · Dec 04, 2010 at 09:59 AM

It's a strange approach to disable a single function in another script to override it. It might be a good place to utilize polymorphism.

If the function is going to be overridden for the whole lifetime of the object it is attached to then you could just use an abstract function with two different specializations. This is how you would do it in C#

base class:

abstract class Base : MonoBehaviour { abstract protected void foo();

 // common functionality

}

specialization #1:

class FirstSpecialization : Base
{
    override protected void foo()
    {
        // do stuff
    }
}

specialization #2:

class SecondSpecialization : Base
{
    override protected void foo()
    {
        // do other stuff
    }
}
Comment
Add comment · 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
1

Answer by BerggreenDK · Dec 04, 2010 at 02:54 AM

I haven't testet it, but I fell quite sure that C# (and the other languages can do this).

here is how C# .NET does it. http://msdn.microsoft.com/en-us/library/ebca9ah3(VS.71).aspx

Same for C# MONO, I believe.

Comment
Add comment · 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
1

Answer by alienmax22 · Jan 21, 2015 at 02:49 PM

Maybe it's too late to answer, but you can use virtual directive

Base class

public class BaseClass : Monobehavior {

     public virtual void MyFunction ()
     {
         Debug.Log("Base function run.");     
     }
 
 }

Child class

public class ChildClass : BaseClass{

     public override void MyFunction ()
     {
         // If you want to run base function as well use base
         base.MyFunction();
 
         Debug.Log("Child function run.");     
     }
 
 }
Comment
Add comment · 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
-4

Answer by Mike 12 · Dec 03, 2010 at 11:29 PM

No, not in Javascript. You can't, because Javascript doesn't have classes. However, you can override functions in C#. Just declare your function as either an abstract or virtual function. With abstract in C#, you only have to declare the function in the parent class. With virtual in C#, you have to implement the method in the parent class and the child classes can optionally override that function.

Comment
Add comment · Show 2 · 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 Mike 12 · Dec 03, 2010 at 11:30 PM 0
Share

$$anonymous$$aybe should have put the "oop" or "inheritance" as a tag for your question so that more people could answer your question.

avatar image Eric5h5 · Dec 03, 2010 at 11:47 PM 3
Share

Sorry, but this is wrong. Javascript does have classes, and they work basically the same way as in C#.

  • 1
  • 2
  • ›

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

1 Person is following this question.

avatar image

Related Questions

Destroying a child object? 1 Answer

Randomly deactivate game objects 2 Answers

How to deactivate objects that are children all at once? 0 Answers

Why does the Unity HingeJoint2D not work when enabled during play? 0 Answers

Script for Revert Value to Prefab 1 Answer


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