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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by FZ_Applications · Jan 02, 2020 at 08:33 PM · inheritancemonobehaviorinheritinherited-members

Strange inheritance behaviour (Two different values)

Hello, I'm currently programming a weapon system with automatic, semi auto, melee and throwable weapons. It is built up using inheritance (see UML diagram). I made a overriden Update loop that increases the fireRateCounter in the AutomaticWeapon class. But when I call Fire() from the Player it doesn't work. But when I call it from AutomaticWeapon's Update or Start it works.

I really dont understand that problem, a solution would be awsome. Thanks you.


https://answers.unity.com/storage/temp/150743-uml.png


 using UnityEngine;
 
 public abstract class Weapon : MonoBehaviour {
 
     public abstract void Fire();
 
     public abstract void Update();
 }
 
 
 public class AutomaticWeapon : Weapon {
 
     public float fireRate = .2f;
     public float fireRateCounter;
 
     public override void Fire() {
         Debug.Log(fireRateCounter); //INCORRECT OUTPUT; Why is it 0???
 
         if (fireRateCounter >= fireRate) {
             Debug.Log("FIRE()");
         }
     }
 
     public override void Update() {
         fireRateCounter += Time.deltaTime;
 
         Debug.Log(fireRateCounter); //CORRECT OUTPUT; Why is it correct here?
     }
 }

 using UnityEngine;
 
 public class Player : MonoBehaviour {
 
     public Weapon weapon;
 
     private void Update() {
         if (Input.GetMouseButton(0)) {
             weapon.Fire();
         } 
     }
 }



uml.png (11.0 kB)
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 Kishotta · Jan 02, 2020 at 09:07 PM 0
Share

I'm not 100% sure what is causing the inconsistency you're seeing, but you do not need to abstract and override the Update method in components. Unity calls it, and the other $$anonymous$$onoBehaviour callbacks via reflection. The override may be causing unexpected/unintuitive things to happen under the hood.

avatar image IINovaII · Jan 02, 2020 at 11:53 PM 0
Share

In the script AutomaticWeapon,fireRateCounter never increases when you call the method Fire() from player. $$anonymous$$aybe you can try something like this.

 public override void Fire() {
          Debug.Log(fireRateCounter);
  
          if (fireRateCounter >= fireRate) {
              fireRateCounter -= fireRate;
              Debug.Log("FIRE()");
          }else{
                 fireRateCounter += Time.deltaTime;
                 //Logic for when the weapon is still in cooldown
          }
               
      }

As suggested by @$$anonymous$$ishotta, it's better to avoid implementing Update method via abstract. As long as your class inherit from $$anonymous$$onobehaviour, you have access to this method from within the class.

1 Reply

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

Answer by Bunny83 · Jan 03, 2020 at 11:51 AM

I can't see any error in the provided code. I would simply assume that this "weapon" variable does not point to the correct object in the scene but rather to a prefab in the project. This is a common mistake and can easily be tested by adding a context object to the Debug.Log statements as second parameter. This way the Unity editor will "ping" / highlight this object whenever you click on the log message in the console. This makes it easier to figure out what object did actually produce this log message.


Also you should always use meaningful and unique log messages so you do not confuse them.

 // use this in Update
 Debug.Log("AutomaticWeapon::Update current fire rate counter = " + fireRateCounter, gameObject);

 // use this in Fire
 Debug.Log("AutomaticWeapon::Fire current fire rate counter = " + fireRateCounter, gameObject);
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 FZ_Applications · Jan 03, 2020 at 12:30 PM 0
Share

Thank you so much, the weapon variable really points towards the prefab, because this script is faulty. Furthermore I will keep your advices in $$anonymous$$d.

 private void StartWeapon(Transform spawnWeapon) {
         Transform newWeapon = Instantiate(spawnWeapon, transform.GetChild(0));
         newWeapon.name = spawnWeapon.name;
         weapon = **spawnWeapon**.GetComponentInChildren<AutomaticWeapon>();
 }

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

123 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 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 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

Component Class Hierarchy 2 Answers

Help with using JsonUtility.FromJsonOverwrite 1 Answer

Using functions of Inherited classes 3 Answers

Particle parent for another particle system 0 Answers

Make a field in inspector hold inheriting classes 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