This is my first unity project so I am fairly unfamiliar with everything the platform has. I am trying to log a message to the console when I have a my player game object run into a finish line. Both objects have Box Colliders on them and I have attached a C# script to the player object. Below is the code I have currently.
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Finish")
{
Debug.Log("Finish");
}
}
The problem is that when I move the player into the "Finish" object no logging appears inside the console.
Thanks in Advance!
This is the main player inspector tab
This is the finish line inspector tab
Your script attached to the player checks for a collision with an object with the tag "Finish". Your Object "Finish Line" has tag "untagged". You have to add a tag "Finish" to it to see it working.
With the updated question and screenshots, the problem is that you're checking for the "Finish" tag but the "Finish" GameObject's tag is set to "Untagged" so the if (col.gameObject.tag == "Finish") statement will not evaluate to true.
You have two options:
1. Select the "Finish" GameObject, click the tag that says "Untagged" and create new tag named "Finish". If you already have this tag, change the tag of the "Finish" GameObject from "Untagged" to "Finish" and your if (col.gameObject.tag == "Finish") code should work.
2. If you did not intend to use tag then just compare the GameObject by name instead of tag by simply replacing if (col.gameObject.tag == "Finish") with if (col.gameObject.name == "Finish").
If none of the two options above worked for you then OnCollisionEnter2D is not being called at-all. Put a Debug.Log outside the if statement like below and leave a comment about if there is a log or not.
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("Finish: " + col.gameObject.name);
}
Just first idea that came in mind:
- Did you add colliders on both of objects that should collide?
Without them engine will not generate events of colliding at all.
Related
I am new to Unity and having problems with this method. Any help would be appreciated.
Currently, I have one box gameObject (customer) that falls down onto another box gameObject (table). The script with theOnCollisionEnter2D method is attached to the tableBox object and checks using tags if the customerBox has collided with it. The problem is that despite colliding, OnCollisionEnter2D does not seem to be getting called at all, as nothing is appearing from Debug.Log. Both boxes have Rigidbody2D and a boxCollider2D.
I have tried changing the Rigidbody2D property to dynamic and also double checking the tags are correct. It is also possible that the script for the tableBox is not running at all, which if is the case I am not sure how to fix and would appreciate suggestions.
Here is the method:
void OnCollisionEnter2D(Collision2D collision){
Debug.Log("in the method.");
if(collision.gameObject.tag == "customer")
{
Debug.Log("Collision detected!");
}
}
Your code seems to be good. Check the following
Both the Rigidbodys are set to dynamic.
Both the bodies have 2D colliders.
They are not colliding already.
Both Colliders are not set as trigger.
Check the layer collision matrix.(Edit>Project settings>Physics2D)
is the "IsTrigger" checkbox unchecked on your collider component ?
Check if the gameobjects are correctly configured (layers, colliders, tags, your script...) and if you got layers, check the physics settings ;)
Unity 2D. I am trying to load EndGameScene when an enemy collides with my player, but I can't seem to get it to work.
My tags are "Player" and "enemy".
This is the current code.
Code:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class EndGame : MonoBehaviour
{
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "enemy")
{
SceneManager.LoadScene("EndGameScene");
}
}
}
I'm new to Unity (and it's been some time since last time I used it), so this check list may be incomplete, but still worth a try:
Put a Debug.Log("it collides!") at the beggining of your OnCollisionEnter function, so you can be sure that it is actually detecting a collition.
If it doesn't print anything:
Check if you are ussing a collider in both the player and the enemies.
The player's collider should have the "Is trigger" checkbox selected, so it triggers a OncollitionEnter event.
If it prints the message:
Check if there is an error message under the printer message, may be that it can't find the scene.
Be sure that you typed correctly the name/tag of the enemies (I suppose the script is on your player)
there's a better way to make the collision:
change the name here if (collision.gameObject.name == "enemy")
and make it like this if (collision.gameObject.tag == "Enemy")
and go to the enemys in you scene and add the Enemy tag to them
It might be because you have not built the scene into the project's build settings (in the unity interface):
Go to the Game Over scene/ open it by going to File>OpenScene>Scenes(folder)>Game Over (scene name)
click 'File' (top left corner)
click 'Build Settings' option
click 'Add Open Scenes button'
make sure the scene you want to add is listed and checked
click 'Build' (on bottom right)
I've made many colliders before and that I used was (collider otherOnTriggerEnter(collider other)) and then check if the other collider is the one I want by comparing TAG. But this time I'm not triggering the canMelee boolean. My code:
ArrayList Enemy;
public StatData stat;
[SerializeField]
private bool canMelee = false;
/// <summary>
/// OnTriggerEnter is called when the Collider other enters the trigger.
/// </summary>
/// <param name="other">The other Collider involved in this collision.</param>
void OnTriggerEnter(Collider col)
{
if (col.tag == "Enemy")
{
Debug.Log("Now you can do melee attack!");
canMelee = true;
}
}
So I wrote this code but somehow this doesn't work I checked the tag and added it to the player game object but it still doesn't work.
I tried to add another debug to check if OnTriggerEnter() is happening just above the if branch : Debug.Log("Triggered"); and it gets triggered.
I can't just figure put what's wrong. Please help me.
First double check that your enemy game objects have the tag "Enemy". Next make sure that your player collider has the "Is Trigger" option selected in the Unity inspector.
Next try using
col.gameObject.CompareTag("Enemy")
instead of: col.tag == "Enemy" in your if statement.
Check out the Unity documentation as well on Colliders:
Unity Collider documentation
Unity Component.CompareTag method with example
Hope this helps!
The first advice I can give you, is ALWAYS, when writting a code Debug it, just make always sure that every step in your code is beeing executed. Inside every if just debug what it is supposed to check and if its true, inside your void, make sure its running the void, etc...
Next:
-Check if you have your object tagged as "Enemy", this can be on of the most common errors, and its so easy to solve, you may have written it wrong, or just not set it up.
-Here is the trigger documentationdocumentation.
-After this, make sure your gameobject is market as triggered.
-If you want you can use also colliders, and this also shows you how to check the tag.
Make sure you set all of this up, if this doesn't helped you, comment bellow, with prints, your gameobjects, codes, etc related to this.
What I'm trying to do is when it hits a triggered collider I want it to minus an int, but what unity does is minus it with 3 instead, and I have it attached to the player itself
lifecontroller lves;
public GameObject gm;
CoinScript coins;
// Use this for initialization
void Start () {
gm = GameObject.FindGameObjectWithTag("GM");
coins = gm.GetComponent<CoinScript>();
lves = gm.GetComponent<lifecontroller>();
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Evil")
{
lves.lives -= 1;
Debug.Log("U hit it");
}
}
// Update is called once per frame
void Update () {
if(lves.lives == 0)
{
Debug.Log("u died");
}
}
Judging from the screenshot, your script is attached to the Player GameObject multiple times. Select your Player GameObject, and remove the duplicate Script.
Make sure there is only one script attached to it. It is probably attached there 3 times.
This problem can also happen if you are instantiating multiple Player GameObjects during run-time as this will duplicate your script too.
EDIT:
With the updated Image in your comment, the problem is that you have 3 Colliders on your Player. It is true that only one of them is made IsTrigger but this is a problem if the GameObject with the Evil tag is marked as IsTrigger too.
You have 2 options to fix this:
1.Select the GameObjects with the Evil tag and make sure that no Collider attached to it is marked as IsTrigger. Unceheck all IsTrigger properties on the Collider of all Evil tagged GameObjects only. This should fix your problem.
2.Only have one Collider on Player GameObject. Remove the other 2. This again should fix your problem.
The trigger is definitly happening 3 times, your screenshot shows that (there is a 3 on the right since duplicate logs get collapsed).
This might occur because of the collider shape I think (circle). Try using a bool that us set on trigger enter and clear on trigger exit (or even Update should be ok for clearing).
This is what I suggested to debug your problem:
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Evil")
{
lves.lives -= 1;
Debug.Log("U hit it", other.gameObject);
}
}
And by looking at your screenshot you are getting 3 log messages.
Always use don't collapse in console window to see whats happening
I'm trying to make a generic "Snake" game using Unity in order to reinforce my knowledge of C# and Unity fundamentals.
The problem I'm having is that I can't seem to get the player to Game Over when colliding with the body. Currently I have the collision check set up like this:
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Border") || other.gameObject.CompareTag("Body")) //if the player collides with a Border tag or Body tag...
{
//Game Over Sequence
GameOver();
}
else if (other.gameObject.CompareTag("Collectable")) //otherwise if the player collides with a Collectable tag...
{
//Snek needs to grow!
shouldGrow = true;
}
}
Here's a picture of the snake body's prefab inspector as well. I checked to make sure the tag is set to Body.
The border collision is working fine, and the strange part is setting the border object to the "Body" tag is also working fine, so I'm not sure why the body segments aren't triggering the Game Over sequence.
I'm new to programming, so apologies if this question is obvious, but I don't understand why this isn't working.
Thanks in advance!
EDIT: Per Programmer's request, here's an image of one of the Border's inspectors:
Tick 'is Trigger' on the Snake_Body's box collider. Without this the OnTriggerEnter2D function is not called. You have done this correctly on the border box collider.