- Home /
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.
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
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.
Your answer
Follow this Question
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