Sorry, I know this has been addressed on here before, [specifically here][1], but for some reason I can’t seem to get it to work. I have two scripts, one on the projectile “S_forwardProjectile.cs” and the other on the enemy “S_weakEnemy.cs”. When the projectile hits the enemy it should reduce the enemies health. I think the problem is something to do with my GetComponent line in “S_forwardProjectile.cs” as that is where I am getting the compile error.
“S_forwardProjectile.cs”
using UnityEngine;
using System.Collections;
public class S_forwardProjectile : MonoBehaviour {
public float projectileSpeed;
public float projectileDamage;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
//Moves projectile.
float amtToMove = projectileSpeed * Time.deltaTime;
transform.Translate(Vector3.up * amtToMove);
//Destroys projectile when off screen.
if (transform.position.y > 8.5f)
Destroy(gameObject);
}
//Damages enemies when projectile hits them.
void OnTriggerEnter (Collider other)
{
//Debug.Log("We hit " + otherObject.name);
if (other.gameObject.tag == "enemy")
{
if (transform.localScale == new Vector3(0.8f, 0.8f, 0.8f))
projectileDamage = 3f;
if (transform.localScale == new Vector3(0.5f, 0.5f, 0.5f))
projectileDamage = 2f;
if (transform.localScale == new Vector3(0.2f, 0.2f, 0.2f))
projectileDamage = 1f;
other.gameObject.GetComponent<S_weakEnemy>().enemyHealth = enemyHealth - projectileDamage;
Destroy(gameObject);
}
}
}
“S_weakEnemy.cs”
using UnityEngine;
using System.Collections;
public class S_weakEnemy : MonoBehaviour {
public float enemyHealth;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (enemyHealth <= 0f)
Destroy(gameObject);
}
}
I tried to go over it several times to make sure I didn’t have a spelling error or something. I am just learning programming, so I am probably just doing something stupid, but for the life of me I can’t figure out what.
Thanks for looking.
[1]: (C#) My enemy dosent take damage ScriptFix - Unity Answers