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
0
Question by Juts · May 12, 2013 at 05:07 AM · c#inheritancesubclass

C# Inheritance Question

I need some help setting up an abstract class. Im confused about class variables and how they inherit, if they even do. Spawntime is being treated as a variable shared by all subclasses. Also, is it possible for the child classes to extend on the abstract classes Update() method? Here is the Abstract Class code I have now:

 public abstract class  AbstractProjectile : MonoBehaviour {
 
 
     public bool DestroyOnTimer = false;
     public bool DestroyWhenInvisible = true;
     public float TimeToLiveInSeconds = 1.0f;
 
     
     //Time the Projectile was created
     private float spawnTime;
 
     // Use this for initialization
     void Start () 
     {
         spawnTime = Time.time;            
     }
     
     // Update is called once per frame
     void Update () 
     {            
         //Destroy the projectile if its timer is over
         if (DestroyOnTimer)            {
             
             if ((Time.time - spawnTime) > TimeToLiveInSeconds)
             {
                 Destroy(gameObject);
             }        
         }
         //Call Child projectiles move method
         move();
     }
 
     public virtual void move() { }
   }


Here is the Child Code:

 using UnityEngine;
 using System.Collections;
 
 public class SinBulletScript : AbstractProjectile {
 
     public float speed = 1000.0f;
     public float Amplitude = 600.0f;
     public float Omega = 15.0f;
 
 
     // Use this for initialization
     void Start () 
     {
         Vector3 shipPosition = GameObject.Find("Ship").transform.position;
         shipPosition.y += 100;
         transform.position = shipPosition;
             
         
     }
         
 
     void move()
     {
         gameObject.rigidbody.velocity = new Vector3(Amplitude * Mathf.Sin(Time.time * Omega), speed, 0);
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Julien-Lynge · May 12, 2013 at 05:09 AM

I only see one question: "is it possible for the child classes to extend on the abstract classes Update() method?", so I'll answer that.

The answer is no. Update is a method that has special meaning to Unity - you can think of it as reserved. You can't redefine Update as abstract. Unity will call your Update function, but you can certainly define another function that gets called from Update - say myUpdate - and do whatever you want with it.

Comment
Add comment · Show 1 · 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 whydoidoit · May 12, 2013 at 06:55 AM 2
Share

Just to point out that Unity finds Update/Awake/Start etc by scanning on the actual type which is created - you can declare any of these methods as protected in a base class and then just add a

    base.Start()

Or whatever to the inherited class. You do not need to make these methods virtual or abstract for this to work (and it saves memory if you don't), but you should declare the method as new in the derived class:

     protected new Start()

So I'd disagree and say you can extend upon the base classes method, but the base class doesn't need it if it's got no functionality - I think @Juts is implying that there should be code in there (rather than that being abstract) and so I'd go with being able to do that.

avatar image
2

Answer by Thom Denick · May 12, 2013 at 05:48 AM

As for your variable question. It's confusing, but private variables are not accesible by children. You need to take a look at the protected keyword, which are variables your children will have access to. I believe this keyword is unique to C#, so if you are coming from other languages, it is probably confusing.

http://msdn.microsoft.com/en-us/library/ms173121(v=VS.80).aspx

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Hide headers and variables in inspector from parent classes 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Inheritance Problems 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