Extending class in c# in elegant way.

Hi,
I have a class(base) which let’s supposed I cannot edit.
But, I need to change 1 public var from this class.

I now, that I can, do it by finding the object, and then accesing the component to edit it’s var.
But is there any way to do it in elegant way ?
I would like to come up with something like this:

// THIS CANNOT BE CHANGED 
public class Base:MonoBehaviour
{
    pubic string name;   
}

// THIS SHOULD CHANGE VALUE OF VAR FROM BASE CLASS
public class ChangeBaseName extend Base
{
    Base.name="New name";       
}

What can you recomend ?

You could try something like:

public static class BaseExtension
{
     public static void ChangeBaseName(this Base self, string newName = "New name")
     {
          self.name = newName;
     }

     public static void DefaultValues(this Base self)
     {
          self.name = "DefaultName";
          self.property = 2;
     }
}

that way you could perform your tasks like:

Base script = gameObject.AddComponent<Base>();

script.ChangeBaseName( "Wololo" );
script.DefaultValues();

Do note you have to call those functions manually.

I’m afraid I don’t understand the difficulty. If we just fix the syntax, what you wrote works perfectly:

// THIS CANNOT BE CHANGED
public class Base : MonoBehaviour
{
    public string name;  
}

// THIS SHOULD CHANGE VALUE OF VAR FROM BASE CLASS
public class ChangeBaseName : Base
{
    void SomeMethod() {
        name="New name";
    }
}

Subclasses can always access any public (or protected) properties in the base class directly. What’s the problem?

1 Like

Since name is a public instance field, it is inherited by ChangeBaseName when you extend Base. Thus there is an instance field in ChangeBaseName called name without doing anything more than

class ChangeBaseName : Base { }

Note that in order to change the value of the field, you need to be in a method. The only field related statements that are permitted outside of a method are variable declarations which can contain an optional initialization value.

So, to explain why your OP doesn’t compile:

  1. You cannot access “base”, much like you can’t access “this”, here because those keywords requires an instance. However, since you are not in an instance method, there will be no instance to operate on.
  2. You cannot create a new field in a derived class with the same name as a field in the base class unless you hide the field in the base class. This is not typically what you want to do because anybody who knows only about the base class will not be able to access your new field which hides the original.

If you want to change the value of name from ChangeBaseName, you would treat it like any normal instance field

class ChangeBaseName : Base
{
  void Awake()
  {
    name = "New name";
  }
}
1 Like

Let me explain the problem in a simpler way:

I have a plugin ( Base ), which is connected to game object.

I need to change 1 var in the plugin, but I don’t want to do it in the script - since it’s being updated often, and it would require changing with each update.

So, I want to create another script, which will make that change automaticly.

I now that working solution is ie.:

void Awake()
{
   this.gameobject.GetComponent<Base>().name="new name";
}

But It would require adding this new script to gameobject with plugin whenever I want to use it.

So maybe now it’s more clear, and somebody has a better idea ?

What I want to do is to has some script which will automate changing this 1 variable externally, without any interference into Base plugin script.

Sounds a bit like a work around for an architecture flaw/misunderstanding.
Can you explain why you want to change the Base field automatically?
The field is public and serializable, so it should show up in the editor. What’s wrong with typing the value in there? Just trying to save keystrokes?

The value which is edited in the Base - is actually name of file being used by this plugin.

User provides the name of the file, and rest of path is added automaticly, depending on the platform build.

Edit

Ok, I just wrote to the plugin author- maybe he will just include our feature in the next update- that’s probably most reasonable solution.
Anyway thanks guys !