- Home /
Question by
Kingdomkey6677 · Oct 02, 2017 at 11:27 PM ·
c#scripting problemraycastreferencingcalling
damage function not calling
My script is not calling a function on another script when I want it to be called. I have tried multiple method and none of them have worked. Here is the script calling the function.
void CheckForInput() {
if (Input.GetButtonDown("Fire1") && Time.time > nextFire) {
if (Physics.Raycast(transform.position, transform.forward, out Hit, range)) {
NavTarget navtarget = Hit.collider.GetComponent<NavTarget>();
if (Hit.collider.tag == "enimy") {
if (navtarget != null)
{
navtarget.AdjustCurentHealth(-10);
}
}
}
}
nextFire = Time.time + fireRate;
}
}
And here is the script with the function.
{
public Transform Gem;
public int maxHealth = 100;
public int curHealth = 100;
public Transform Target;
private NavMeshAgent agent;
int numGems = 5;
// Use this for initialization
void Start()
{
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
agent.SetDestination(Target.position);
Vector3 forward = transform.TransformDirection(Vector3.forward) * 3;
Debug.DrawRay(transform.position, forward, Color.red);
}
public void AdjustCurentHealth(int adj)
{
curHealth += adj;
if (curHealth > maxHealth)
curHealth = maxHealth;
if (maxHealth < 1)
maxHealth = 1;
if (curHealth < 0)
curHealth = 0;
if (curHealth < 10)
Destroy(gameObject);
for (int i = 0; i < numGems; i++)
{
Instantiate(Gem, gameObject.transform.position, Quaternion.identity);
}
}
}
On a side note, should I keep my navigation script and health script separate? Thanks in advance!
Comment
Your answer
Follow this Question
Related Questions
Targeting system, can select but won't deselect... 0 Answers
setting a bool on another object's animator by player's raycast 2 Answers
Distribute terrain in zones 3 Answers
How can I make that the interactable raycast shoot will not pass through some objects like doors ? 1 Answer
How can i use raycast hit to detect object by the object tag name ? 1 Answer