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 SeaBassTurd · Feb 29, 2020 at 01:46 PM · c#scripting probleminheritancegame developmentstealth

inheritance - problem setting class variable from external call

I am trying to implement sound detection for stealth in a game, but I can't seem to trigger an event in the Enemy's script that gives it the location of the sound/noise when the Player performs some action.
I have a variable _detectedAudio defined in a parent class base_entity (archetype for all Enemies) that is inherited by a class entity_keeper (specific Enemy) which is added to the Enemy GameObject in the Scene. From the Player's script, I am getting the Enemy GameObject's script as either base_entity (parent class) or entity_keeper (child class), and calling the base_entity function Register_AudioPoint(). When I typecast as base_entity, its showing that the function is being called and _detectedAudio's value has been changed. However when I try to access _detectedAudio in the Enemy's script entity_keeper while its running, its showing the old value, unchanged. When I tried typecasting as entity_keeper instead, it seems to work fine.

Player's script (base_player : MonoBehaviour):

 public void Walk()
 {
     // * testing walk noise alert
     Collider[] hitColliders = Physics.OverlapSphere(Position, MoveVector.magnitude * test_loudness);
     // Collider[] hitColliders = Physics.OverlapSphere(Position, test_loudness);
     for (int i = 0; i < hitColliders.Length; i++)
         if (hitColliders[i].gameObject.layer == game_variables.Instance.LayerEntity)
             // hitColliders[i].transform.GetComponent<base_entity>().Register_AudioPoint(transform.position);
             hitColliders[i].transform.GetComponent<entity_keeper>().Register_AudioPoint(transform.position);
             // Debug.Log(hitColliders[i].gameObject.name);
 }

Event function (base_entity : MonoBehaviour):

 public void Register_AudioPoint(Vector3 source)
 {
     // Debug.Log("heard");
     // AudioEvent event;
     // event.point = source;
     // event.intensity = 
     // _audioPoints.Add(source);
     // Debug.Log(Time.time + "\t" + _audioPoints.Count);
     
     // alert to nearest/loudest source
     // if (Vector3.Distance(source, transform.position) < Vector3.Distance(_detectedAudio, transform.position))
     // {
     _detectedAudio = source;
     _attentionTimer = _attentionTime;
         // Debug.Log("there~\t" + point);
     // }
 }

Enemy GameObject's script (entity_keeper : base_entity):

 protected override void State_Idle()
 {
     // alive...
     if (base_player.Instance.IsAlive)
     {
         // Debug.Log(_detectedAudio + "\t" + Vector3.Distance(_detectedAudio, player_motor.Instance.Position));
         // spotted...
         if (_detectedVision == player_motor.Instance.Position)
         {
             _positionMove = _detectedVision;
             // ...engage
             _state = State.ENGAGE;
         }
         // .!.heard... *borked
         // else if (_detectedAudio == player_motor.Instance.Position)
         else if (Vector3.Distance(_detectedAudio, player_motor.Instance.Position) < 1f)
         {
             _positionMove = _detectedAudio;
             // ...engage
             _state = State.ENGAGE;
         }
     }
     else
         // .!.keep moving|recite|struggle
         // _audio.Play("verse");
         _state = State.PATROL;
 }

I would prefer to be able to use the parent class typecast but I can't seem to get it to work. The only other alternatives I can think of are to either use tags to call each specific Enemy's script, since I don't have that many in the game (about 10-12). Or having an overriden version of Register_AudioPoint() in each of the child classes, but that'd be needlessly redundant. If anyone can suggest a solution or better alternative that'd be great.
Thanks in advance.

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 Masterio · Feb 29, 2020 at 02:41 PM 0
Share

I am not sure if I undestood but... You can read about the 'virtual' methods. https://docs.microsoft.com/en-gb/dotnet/csharp/language-reference/keywords/virtual

avatar image Namey5 · Mar 01, 2020 at 05:49 AM 0
Share

Are there any other classes that derive from base_entity also attached to the keeper object? From the description it sounds like it's finding a different instance of base_entity and accessing that ins$$anonymous$$d of the intended class. If that was the case, then you would either need to typecast some kind of middleground class to separate the two, or find all base_entity components and iterate through them setting the audio's position.

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Unity 3D Player Follow 0 Answers

Inheritance Implementation Weapon add System for the Pro Military: FPS Character? 0 Answers

I need to make two bullet controllers (friendly and enemy) Should I inherit? 1 Answer

Multiple Cars not working 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