• 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 /
avatar image
0
Question by Andreapigna · Mar 26, 2014 at 02:58 PM · gameobjectdestroytaggameobject.find

GameObject.FindWithTag doesn't work!

Here's my script:

 #pragma strict
 
 var CrystalHealth : float = 100;
 var Effect : GameObject;
 var GemSpawner : GameObject;
 
 function Update() {
 var Bomb = GameObject.FindWithTag("Bomb");
 
 if (CrystalHealth<=0) {
 Instantiate(GemSpawner, this.transform.position, Quaternion.identity);
 Destroy(this.gameObject);
 }
 }
 
 function OnCollisionEnter(Bomb : Collision) {
 CrystalHealth=CrystalHealth-100;
 Instantiate(Effect, this.transform.position, Quaternion.identity);
 }

When the object who has the script attached to it collides with...anything, also with object that hasn't the tag "Bomb", the CrystalHealth goes to 0 and the object destroys himself. How can i fix this? I've also tried using GameObject.FindGameObjectWithTag and GameObject.Find. Nothing. Please help me!

Comment
Add comment · Show 1
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 trololo · Mar 26, 2014 at 03:11 PM 0
Share
 var Bomb = GameObject.FindWithTag("Bomb");

Do that in your Start() or just set it in the editor (attribut should be public then).

 Instantiate(Effect, this.transform.position, Quaternion.identity);

Same thing, don't instatiate here. Place your effect inside the GameObject wanted, and active/desactive it in your OnCollisionEnter() function (by the way you should use OnTriggerEnter here i guess).

 Instantiate(GemSpawner, this.transform.position, Quaternion.identity);

Same.

Do that first.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ed unity · Mar 26, 2014 at 03:27 PM

Your problem does not appear to be with GameObject.FindWithTag(). The parameter passed into OnCollisionEnter is not always going to be the bomb. Every object that collides with the object this script is attached to will be passed into the OnCollsionEnter function. Your tag check should be placed in the OnCollisionEnter function and if it is a "Bomb" you proceed, otherwise return.

 function OnCollisionEnter(otherObject : Collision)
 {
 if(otherObject.gameObject.tag == "Bomb")
 {
 CrystalHealth=CrystalHealth-100;
 Instantiate(Effect, this.transform.position, Quaternion.identity);
 }
 }

The var Bomb variable you created is completely separate from the variable passed as the parameter in the OnCollsionEnterFunction.

Comment
Add comment · Show 1 · 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 Andreapigna · Mar 26, 2014 at 03:43 PM 0
Share

Thank you! It worked!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

When all objects with a certain tag has been destroyed, load the next level! 3 Answers

how do i create a game object array with gameobjects of multiple tags 1 Answer

How do I make Destroy(gameObject) work in my script? 1 Answer

create a destroyer for 2d infinite runner game objects. 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges