Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by zefrof · Jan 07, 2013 at 01:40 PM · c#ontriggerentertags

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: alt text

Here is the inspector of the GameObject with the script on it: alt text

screen shot 2013-01-07 at 6.34.33 pm.png (49.9 kB)
screen shot 2013-01-07 at 6.34.07 pm.png (45.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pako · Jan 09, 2013 at 08:30 AM 0
Share

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

avatar image zefrof · Jan 09, 2013 at 08:47 PM 0
Share

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?

avatar image pako · Jan 09, 2013 at 09:53 PM 0
Share

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.

avatar image pako · Jan 09, 2013 at 10:54 PM 0
Share

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!");}

avatar image zefrof · Jan 09, 2013 at 11:00 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges