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 Codelyy · Jun 21, 2016 at 02:48 AM · variableinheritance

Small Inheriting Problem

I've never really done anything with Inheriting so I'm expecting the fix to this problem to be really clear to everyone else but I basically made a projectile base class to handle all the projectile movement for bullets and lasers and so on.

This is what I've got in the projectile base class:

 using UnityEngine;
 using System.Collections;
 
 public class Projectile : MonoBehaviour {
 
     public float projectileSpeed;
     public float damageAmount;
         
     // Update is called once per frame
     protected virtual void FixedUpdate () 
     {
         ProjectileMovement();
     }
 
     protected virtual void ProjectileMovement()
     {
         transform.position = transform.position + Vector3.up * projectileSpeed * Time.deltaTime;
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if(other.tag == "Enemy")
         {
             Destroy(gameObject);
         }
     }
 }

I then created a class called PlayerBullet and had it inherit projectile:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerBullet : Projectile {
 
     protected float moveSpeed;
     protected float damage;
     
     // Use this for initialization
     void Update () {
         this.projectileSpeed = moveSpeed;
         this.damageAmount = damage;
     }
 
 }
 

I'm basically trying to make it so I can input separate projectile speeds and damage amounts for different projectiles so I created separate variables within the playerbullet class so I could assign them in the inspector and then have the projectileSpeed and damageAmount variables within the base class equal the same as the related variables within the playerbullet class. So as you see: "projectileSpeed = moveSpeed" is within the playerbullet class as I thought that would assign the moveSpeed varaible to the projectileSpeed variable within the base class but no matter what I do, the projectile speed within the projectile base class will always be zero and I'm not sure why.

As I said I'm new to this so I'm sorry for the most likely nooby question and I hope what I said makes sense XD

Can anyone help me?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by JDAUL · Jun 21, 2016 at 04:02 AM

You need to initialize your moveSpeed and damage variables in your PlayerBullet class.

 using UnityEngine;
  using System.Collections;
  
  public class PlayerBullet : Projectile {
  
     //Change these values to what you want
      protected float moveSpeed = 5f;
      protected float damage = 10f;
      
      // Use this for initialization
      void Update () {
          this.projectileSpeed = moveSpeed;
          this.damageAmount = damage;
      }
  
  }

However, it would also be much simpler to just do this:

 using UnityEngine;
  using System.Collections;
  
  public class PlayerBullet : Projectile {
 
      // Use this for initialization
      void Start() {
        //Change these to what you want
          this.projectileSpeed = 5f;
          this.damageAmount = 10f;
      }
  
  }


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

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

Related Questions

How to check if variable exists? 1 Answer

HideInInspector with inherited variables 1 Answer

Is it possible to set variables for classes? 2 Answers

Parent Class variables in Child Class's Inspector (C#) 0 Answers

Using a script as a member variable in another script. 2 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