public and private problem
using UnityEngine;
using System.Collections;
namespace CompleteProject
{
public class EnemyAttack : MonoBehaviour
{
public float timeBetweenAttacks = 0.5f; // The time in seconds between each attack.
**public int attackDamage = 20;** // The amount of health taken away per attack.
Animator anim; // Reference to the animator component.
GameObject player; // Reference to the player GameObject.
PlayerHealth playerHealth; // Reference to the player's health.
EnemyHealth enemyHealth; // Reference to this enemy's health.
bool playerInRange; // Whether player is within the trigger collider and can be attacked.
float timer; // Timer for counting up to the next attack.
void Awake ()
{
// Setting up the references.
player = GameObject.FindGameObjectWithTag ("Player");
playerHealth = player.GetComponent <PlayerHealth> ();
enemyHealth = GetComponent<EnemyHealth>();
anim = GetComponent <Animator> ();
}
void OnTriggerEnter (Collider other)
{
// If the entering collider is the player...
if(other.gameObject == player)
{
// ... the player is in range.
playerInRange = true;
}
}
void OnTriggerExit (Collider other)
{
// If the exiting collider is the player...
if(other.gameObject == player)
{
// ... the player is no longer in range.
playerInRange = false;
}
}
void Update ()
{
// Add the time since Update was last called to the timer.
timer += Time.deltaTime;
// If the timer exceeds the time between attacks, the player is in range and this enemy is alive...
if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
{
// ... attack.
Attack ();
}
// If the player has zero or less health...
if(playerHealth.currentHealth <= 0)
{
// ... tell the animator the player is dead.
anim.SetTrigger ("PlayerDead");
}
}
public void Attack ()
{
// Reset the timer.
timer = 0f;
// If the player has health to lose...
if(playerHealth.currentHealth > 0)
{
// ... damage the player.
print("Attackdamage == " + attackDamage);
**playerHealth.TakeDamage (attackDamage);**
}
}
}
}
and the problem is at the [*] position
first if i use public ... the variable will not work in the next [*] one...
but if i change public attackdamage to private attackdamage and it works now
I want someone that can explain to me why this is happen and what cause it to be like this
thank you
Answer by NoseKills · Oct 18, 2017 at 04:31 PM
What do you mean by "doesn't work"? Health doesn't decrease? Or does it give an error?
Without knowing more, my first guess is that when you first created the variable attackDamage, you didn't assign a default value for it so it got serialized into a prefab with value 0. Now every time you make it public, it becomes a serialized variable that gets this 0 value from the prefab. When you make it private, the assigned value 20 doesn't get overridden by the serialized value and it 'works'.
If this is the case, make it private, save the project, make it public and save again. This should erase and rewrite the serialized value in the prefab.
Or if you really want to hardcode it to 20 instead of using the inspector, leave it private or use the [HideInInspector] attribute so you don't serialise its value.
Answer by Krieger9 · Apr 16, 2018 at 10:52 PM
When you change a variable to public the editor takes control of it. If you want to set the default value you need to find the object and select it so you can see the script component and assign the value there.
Otherwise you need to set it later after the engine has already initialized it.
Your answer