- Home /
What's wrong with my code? C#
I have this code:
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:
Here is the inspector of the GameObject with the script on it:
Answer by pako · Jan 08, 2013 at 07:20 PM
Hi Zezfrof,
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:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html
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:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindWithTag.html
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:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html
(the 1st example has a typo and might confuse you)
Let me now if something is not clear.
Hi zefrof,
If this answer helped you please click the check mark for the answer (located just below the down vote symbol). This way the system marks the question as "answered" (or the system will think this is unanswered forever).
Please read the FAQ:
http://answers.unity3d.com/page/faq.html
It is a user guide for "Unity Answers".
Thank you
I didn't specify enough what I'm trying to do so let me iterate. I want the code to detect when anything with the tag "Weapon" collides with the object the code is attached to. Am I even going about it right?
Let's analyze this:
Here's what the code you have attached does:
You have created a Class named "EnemyHealth"
This class contains code for how a collider -set as a trigger- will react (behave), whenever another object whose collider "other" -as identified by the parameter of the OnTriggerEnter(Collider other) function- hits the trigger.
For EnemyHealth script to "come alive" it must be attached to a GameObject in the scene.
e.g. you have a GameObject in the scene named "Player", and you attach "EnemyHealth" on it. Whenever another GameObject, in the scene with a collider hits "Player":
a. The OnTriggerEnter(Collider other) function will run.
b. The parameter "other" will contain information about the collided GameObject's collider.
c. Let's assume now that you use if(GameObject.FindWithTag("Weapon")) inside the OnTriggerEnter(Collider other) function, just for argument's sake.
This way, you don't make use of the really useful information contained in the "other" parameter of the function. So, in effect you ignore the actual GameObject that collided with player, and all you do upon the collision, is you start looking in your scene for any GameObject with the tag "Weapon". If one is found then if(GameObject.FindWithTag("Weapon")) will be evaluated as if(true), and the Debug.Log("IT WOR$$anonymous$$S!"); will execute. Of course, the gameObject with the tag "Weapon" may, or may not be the one collided with the "Player".
So, I think you can see yourself now that this is not what you want.
From the new info you supplied, in order to do what you want, you need to make use of the "other" parameter, which holds information about the collided object:
if (other.gameObject.tag = "Weapon") {Debug.Log("IT WOR$$anonymous$$S!");}
I hope this is clear. If still in doubt let me know.
Small correction: Inside the if() you must use the double equal operator '==', and not '=', which is only used for assignment.
Sorry for this, but I always make this mistake, and the compiler corrects me. So the if() statement should be:
if (other.gameObject.tag == "Weapon") {Debug.Log("IT WOR$$anonymous$$S!");}
So I changed the code to this:
if(other.gameObject.tag == "Weapon") { Debug.Log("IT WOR$$anonymous$$S!");
I tried having the player being the "Weapon" and the enemy having the script and vise versa neither worked. :( I'm not sure what is wrong. If you need some pics of my editor or something else let me know.
Your answer
Follow this Question
Related Questions
Collision reaction not working 2 Answers
Destroy object with tag array OnTriggerEnter C# 1 Answer
I'm trying to adjust tagged objects meshrenderer in C# code. 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers