I want to pass my score variable (which is a static variable) to this function in this class itself by accessing the class CharacterAnimationDelegate…
It show no errors after i build it but it does not work when i tested it in the game.
The general idea is if the player reaches a specific amount of scores, the wall (which this script is attached to ) will be destroyed.
public class CharacterAnimationDelegate : MonoBehaviour
{
public GameObject left_Arm_Attack_Point, right_Arm_Attack_Point,
left_Leg_Attack_Point, right_Leg_Attack_Point;
public float stand_Up_Timer = 1.5f;
public static int score = 0;
This is the first 13 lines of my CharacterAnimationDelegate class (to show the variables i try to acesss)
Thank you for your time to read here.
its a 3d beat em up game where defeating an enemy adds 1 point…
there are several roadblocks along the way… player must defeat several enemies (each enemy 1 point) to get pass through the roadblocks…
i’ll give a full code for CharacterAnimationDelegate (just focus on CharacterDied())
public class CharacterAnimationDelegate : MonoBehaviour
{
public GameObject left_Arm_Attack_Point, right_Arm_Attack_Point,
left_Leg_Attack_Point, right_Leg_Attack_Point;
public float stand_Up_Timer = 1.5f;
public static int score = 0;
private CharacterAnimation animationScript;
private AudioSource audioSource;
[SerializeField]
private AudioClip whoosh_Sound, fall_Sound, ground_Hit_Sound, dead_Sound;
private EnemyMovement enemy_Movement;
private ShakeCamera shakeCamera;
void Awake()
{
animationScript = GetComponent<CharacterAnimation>();
audioSource = GetComponent<AudioSource>();
if(gameObject.CompareTag(Tags.ENEMY_TAG))
{
enemy_Movement = GetComponentInParent<EnemyMovement>();
}
shakeCamera = GameObject.FindWithTag(Tags.MAIN_CAMERA_TAG).GetComponent<ShakeCamera>();
}
// left arm settings
void Left_Arm_Attack_On()
{
left_Arm_Attack_Point.SetActive(true);
}
void Left_Arm_Attack_Off()
{
if (left_Arm_Attack_Point.activeInHierarchy)
{
left_Arm_Attack_Point.SetActive(false);
}
}
// right arm settings
void Right_Arm_Attack_On()
{
right_Arm_Attack_Point.SetActive(true);
}
void Right_Arm_Attack_Off()
{
if (right_Arm_Attack_Point.activeInHierarchy)
{
right_Arm_Attack_Point.SetActive(false);
}
}
// left leg settings
void Left_Leg_Attack_On()
{
left_Leg_Attack_Point.SetActive(true);
}
void Left_Leg_Attack_Off()
{
if (left_Leg_Attack_Point.activeInHierarchy)
{
left_Leg_Attack_Point.SetActive(false);
}
}
// right leg settings
void Right_Leg_Attack_On()
{
right_Leg_Attack_Point.SetActive(true);
}
void Right_Leg_Attack_Off()
{
if (right_Leg_Attack_Point.activeInHierarchy)
{
right_Leg_Attack_Point.SetActive(false);
}
}
// quick tag for left arm
void TagLeft_Arm()
{
left_Arm_Attack_Point.tag = Tags.LEFT_ARM_TAG;
}
// quick untag for left arm
void UnTagLeft_Arm()
{
left_Arm_Attack_Point.tag = Tags.UNTAGGED_TAG;
}
// quick tag for left leg
void TagLeft_Leg()
{
left_Leg_Attack_Point.tag = Tags.LEFT_LEG_TAG;
}
// quick untag for left leg
void UnTagLeft_Leg()
{
left_Leg_Attack_Point.tag = Tags.UNTAGGED_TAG;
}
// for activating time for enemy to stand up after being knocked down..
void Enemy_StandUp()
{
StartCoroutine(StandUpAfterTime());
}
// counter
IEnumerator StandUpAfterTime()
{
yield return new WaitForSeconds(stand_Up_Timer);
animationScript.StandUp();
}
// attack FX function
public void Attack_FX_Sound()
{
audioSource.volume = 0.2f;
audioSource.clip = whoosh_Sound;
audioSource.Play();
}
void CharacterDiedSound()
{
audioSource.volume = 1f;
audioSource.clip = dead_Sound;
audioSource.Play();
}
void Enemy_KnockedDown()
{
audioSource.volume = 0.5f;
audioSource.clip = fall_Sound;
audioSource.Play();
}
void Enemy_HitGround()
{
audioSource.volume = 0.5f;
audioSource.clip = ground_Hit_Sound;
audioSource.Play();
}
void DisableMovement()
{
enemy_Movement.enabled = false;
// set enemy parent into default layer
transform.parent.gameObject.layer = 0;
}
void EnableMovement()
{
enemy_Movement.enabled = true;
// set enemy parent into Enemy (original) layer
transform.parent.gameObject.layer = 10;
}
void ShakeCameraOnFall()
{
shakeCamera.ShouldShake = true;
}
// when enemy character died
void CharacterDied()
{
Invoke("DeactivateGameObject", 2f);
score = score + 1;
Debug.Log("Your Score : " + score);
}
// to remove the enemy from game after defeated
void DeactivateGameObject()
{
// EnemyManager.instance.SpawnEnemy();
gameObject.SetActive(false);
Destroy(this.gameObject);
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
and the class for the Area1stBox (the first roadblock which requires the score of 1 to pass through it)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Area1stBox : MonoBehaviour
{
void Awake()
{
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
DestroyWall();
}
void DestroyWall()
{
if (CharacterAnimationDelegate.score == 1)
{
Debug.Log("Reached score 1!!");
Destroy(this.gameObject);
}
}
It seems like you are accessing the score variable correctly. You may want to change if (CharacterAnimationDelegate.score == 1) to if (CharacterAnimationDelegate.score >= 1)to make sure you are not skipping over score == 1. Also make sure that Area1stBox is actually calling its update function and that CharacterAnimationDelegate is actually calling CharacterDied()
Looks like this CharacterAnimationDelegate is an enemy 's component. Pretty weird implementation. Scores should be stored and maintained on a player, not on enemies.