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

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

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.

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.");     
    }

}

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

Rather than just simply trying to correct me and voting down my answer you could've tried to understand what I was saying. JavaScript doesn't have true classes.

It has syntax that's similar to Java's syntax for accessing an instance of a class's attributes and methods, so as to be easier for web programmers who were already familiar with Java, but the semantics is different.

I know that I read in one of my textbooks a whlie back that Javascript doesn't use classes, but unfortunately I don't remember where it is so I can't reference it. However, I did happen to find an article online where the author's statements support my argument.

JavaScript is generally a prototype-based language; where each object is not constructed from a predefined template of attributes and methods (i.e. a class), but is effectively just a dynamic associative array of attribute names and values...classical inheritance; defines one class as a subclass of another thus inheriting its methods and attributes. When a class instance (i.e. an object) is created, that object has all the attributes and methods of the class' entire inheritance hierarchy. With JavaScript's prototype inheritance, an object may point to another object as its "prototype"...A big difference, however, is that prototypes, being simple objects, may be changed at runtime and hence dynamically change what is being inherited.

Source: Implementing "Real" Classes in JavaScript, by Bruce Wallace (February 1, 2007). Developer.com. Retrieved on December 5, 2010 from: Implementing-Real-Classes-in-Javascript

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.