using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
private float EnemyLife;
private float Damage;
// Use this for initialization
void Start () {
EnemyLife = 150f;
Damage = 25f;
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other) {
if(GameObject.FindGameObjectWithTag("Weapon")) {
Debug.Log("IT WORKS!");
}
}
}
I’m new to codding, but I’m pretty sure I’m doing something wrong in the parenthesis of OnTriggerEnter, but I don’t know what. I’m not using EnemyLife and Damage yet because I want to get the Debug to run first. The box I’m having collide does have the tag Weapon. If you have any questions I’ll do my best to answer them. Thanks for all the help!
EDIT 1:
Here is the inspector of the Weapon:
[6476-screen+shot+2013-01-07+at+6.34.33+pm.png|6476]
Here is the inspector of the GameObject with the script on it:
[6477-screen+shot+2013-01-07+at+6.34.07+pm.png|6477]
GameObject.FindGameObjectWithTag(“Weapon”) will return a list of GameObjects (an array to be more exact), i.e. the return type of this function is GameObject. For more details see the documentation:
But inside the if() statement you need a bool variable, or a function that returns a bool. Also, you can use a SINGLE object inside the if() to see if the object exists, but you can’t use a LIST (or array) of objects inside the if(), i.e. if the object is null, it is treated as false, if it’s not null it’s treated as true.
You can use GameObject.FindWithTag inside the if() as this function returns a single GameObject. See the documentation:
However, I’m not sure if this suits your purposes. So, if you do need to use a list (or array), then you have to assign the result of the GameObject.FindGameObjectWithTag function to a variable of type GameObject, and then use foreach statement to iterate though the list items, and use each item inside the if() statement if you want. For usage see the 2nd example in this:
(the 1st example has a typo and might confuse you)