- Home /
OnTriggerEnter or If statement problem
Hello, everyone.
I'm not a native English speaker, so please be generous with my grammatical faults.
Hello,again.
I'm working on 2D game like BubbleBobble(by TAITO), using Unity 4.3 version.
Here's simple rules in my game.
(1) Player blows bubbles, and Enemy meets bubbles, it's captured in bubbles.
(2) Once Enemy is captured in a bubble, it will be destroyed when Player hit it.
So, Enemy has two animations; "Idle" and "Captured", also it has a transition condition Boolean type "IsCaptured", and is set false as default value.
(1) works successfully, but (2) goes strange way, and I don't know why.
Please see my script codes below.
function OnCollisionEnter(col: Collision) {
myAnim.SetBool("isCaptured",true);
transform.Translate(Vector2.up * Time.deltaTime);
this.collider.isTrigger = true;
}
function OnTriggerEnter(other: Collider)
{
if(other.gameObject.tag == "Player" && myAnim.GetBool("isCaptured") == true)
{
Destroy(this.gameObject);
}
}
Problem is, even though the "if statement", all the Enemies are destroyed when they collide with Player!
I can't find where is wrong.
I think it doesn't have any logical errors, but I'm not sure.
Please, help me.(T_T)
I'm so exhausted to find out alone.(T_T)
could you show where you getting from "myAnim" variable? as it is not clear where its co$$anonymous$$g from Are you setting originally ""isCaptured" to false?
"myAnim" variable is in a script attached in Enemy.
var myAnim:Animator;
Also, I set its value is false in a function Start() like...
function Start() {
myAnim = GetComponent(Animator);
myAnim.SetBool("isCaptured",false);
}
also did in a Awake(), but the result was same. Enemy's animation works well, so I didn't consider about that.
Answer by Melih Şimşek · Dec 17, 2013 at 03:34 PM
I'm not sure what you mean by 'Enemy'. If the Enemy is a Prefab; after destroying Prefabs do not exist inside the scene, they are in the project. You should not be destroying Prefabs, will effect each other.
if you use
Destroy(this.gameObject);
it'll destroy the gameobject in which you have placed the script....
Destroy(gameObject);
it'll destroy particular gameobject what you have specified.
Oh..thank you for the answer. However, I'm afraid to say it's kind of different problem. It seems my explain doesn't have accurate information enough.
First, "Enemy"is a game object, which is prefab. Second, the problem is not "Enemy is destroyed all together at the same time".
The problem is "Enemy must be destroyed only satisfied two conditions; (1) it's in a bubble, (2) it collide with Player." However, When a Player collide with an Enemy, the Enemy is just destroyed. It shouldn't be, because the Enemy is not captured in a bubble.
I checked the state of Enemy's animator is clearly "IsCaptured", and also checked the collide with Player. I can't find why...(T_T)
Answer by fancytank · Dec 18, 2013 at 07:15 AM
Is there anyone have any idea about this, anymore? T_T
Unity's expert doesn't check these questions? T_T
Your answer
![](https://koobas.hobune.stream/wayback/20220613124341im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
if statement for 6 ints help C# 2 Answers
Help returning a true varaible 4 Answers
Can't call GetKey inside OnTriggerEnter? 2 Answers