Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by gsunity · Jul 20, 2011 at 05:47 PM · c#inheritancederived-class

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

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 Dreamblur · Jul 20, 2011 at 06:12 PM 0
Share

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

avatar image SilverTabby · Jul 20, 2011 at 06:43 PM 0
Share

Unity EXTENDS C#, not OVERWRITES it. If it works in a pure C# environment, it should work in Unity.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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