- Home /
C# How to derives A from B, where B in different .cs file
I have question about inheritance of c# in Unity 3D.
Let's say, below is my first file, parent.cs :
using System;
public abstract class parent {
private string _name;
public parent() {
_name = "my name is ";
}
public void appendName(string _appendThis) {
_name += _appendThis;
}
public string getName() {
return _name;
}
}
Then, below is my second file, child.cs :
using UnityEngine;
using System;
public class child : parent {
public child() {
base.appendName("gsunity");
}
public string SayName() {
return base.getName();
}
}
Then I have last file, myunity.cs :
using UnityEngine;
using System;
public class myunity : MonoBehaviour {
private child guy = new child();
void Start() {
Debug.Log(guy.SayName());
}
}
My Question is, how to using another cs file in Unity 3D, like three c# script above as the example?
Edited in July 21, 2011.
There was a lot of wrong statement in my script before. So I alter all above script after I finish downloading Unity and tried it.
The parent class must declared like this: public abstract class parent I was forgot to write public keyword before the abstract keyword.
Now my script can work as it should. I just had to Drag and Drop the myunity.cs to an object that I created before. And the other script (parent.cs and child.cs) was run automatically.
My problem now has been solved. Thank's all
Wait, what? $$anonymous$$
Are you asking how to do exactly that? If that's what you're asking, then the answer is to do exactly that. =/
Unity EXTENDS C#, not OVERWRITES it. If it works in a pure C# environment, it should work in Unity.
Answer by Peter G · Jul 20, 2011 at 06:28 PM
Well for starters you can't instance an abstract
class, but I assume that's just a typo so I'll move onto the actual question.
Your scripts should work fine. The only thing I could see that could cause issues it the compilation order. You want to make sure that the parent and child classes are compiled on the same level, and the myunity
class is compiled on the same level or after.
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a static class derive from MonoBehaviour in C# 3 Answers
Is it possible to change value on base class when the same field gets changed on it's derived class 0 Answers