Same code recognizes tag but does not run if statement in different script.
I am making this enemy that copies the player's actions. So I also want the enemy to interact with this platform the same way the player does, so I copied the code from the player to the enemy. But the same code in the enemy's script recognizes the tag but does not run the if statement. However, it has been working fine in the player's script.
The Player's, the one that works:
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Semi-Solid Platform")
{
Vector3 dir = (other.gameObject.transform.position - gameObject.transform.position).normalized;
if (dir.y > 0)
{
tempPos.y += 0.6f;
rigidbody2d.velocity = Vector2.up * 20;
transform.position = tempPos;
}
}
}
The enemy's, the one that does not work:
void OnCollisionEnter2D(Collision2D other)
{
if (Player.transform.position.x < transform.position.x - 4 | Player.transform.position.x > transform.position.x + 4)
{
if (Player.transform.position.x > transform.position.x - 18)
{
if (other.gameObject.tag == "Laser")
{
audioManager.Play("Laser Hit");
Destroy(gameObject);
Instantiate(EnemyEffect, transform.position, transform.rotation);
}
}
}
if (other.gameObject.tag == "Semi-Solid Platform")
{
Vector3 dir = (other.gameObject.transform.position - gameObject.transform.position).normalized;
if (dir.y > 0)
{
tempPos.y += 0.6f;
rigidbody2d.velocity = Vector2.up * 20;
transform.position = tempPos;
}
}
Debug.Log(other.gameObject.tag);
}
Never $$anonymous$$d, I figured it out myself, I jest needed to change "tempPos.y += 0.6f;" to "tempPos.y += 1f;" to account for the enemy's size difference.
Your answer
![](https://koobas.hobune.stream/wayback/20220613051110im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I need your help for SaveLoad method. 1 Answer
[C#] Enum not working properly 2 Answers
Expanding onto the 2D RougeLike Game adding a main menu (URGENT! help required please) 1 Answer
Nulante Anti-Aging Cream :Achieve Visibly Younger Looking Skin Today 0 Answers
[HELP] List instance not being destroyed OnTriggerExit 2 Answers