OnTriggerEnter2D is not being called - c#

I am trying to make a replica of Asteroids in Unity. The problem is that my bullets are not triggering the OnTriggerEnter2D method on the asteroids. The asteroids have the following script attached:
using UnityEngine;
using System.Collections;
public class Asteroid : MonoBehaviour {
void Start () {
print ("class initiated");
}
void onTriggerEnter2D (Collider2D collider) {
Debug.Log (collider);
}
}
The bullet GameObject has Is Kinematic and Is Trigger enabled, and has Rigidbody 2D and Box Collider 2D attached. The asteroid GameObject has Rigidbody 2D and Circle Collider 2D, and Is Kinematic and Is Trigger is unchecked.

The problem is the Spelling. The o in onTriggerEnter2D should be capitalized. Simple mistake like this one can ruin your day. I didn't even notice it until I ran your code.
void OnTriggerEnter2D(Collider2D collider)
{
Debug.Log(collider);
}
Next time if you are not sure about the spelling of the Unity callback function, right click in Visual Studio then click Implement MonoBehaviours search for the function you want, select it and click OK. Visual Studio will add that function for you.

Related

How can I check multiple collider Triggers at once?

I have an aircraft in my game that is controlled by the player. I want it to get destroyed after it collides with something. The aircraft has many colliders on it, so im using an array. Heres my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AircraftDamage : MonoBehaviour
{
public Collider[] Colliders;
public GameObject Aircraft;
private void OnTriggerEnter(Colliders collision)
{
DestroyAircraft();
}
public void DestroyAircraft()
{
Destroy(Aircraft);
}
}
Im getting the obvious error here, type or namespace 'Colliders' could not be found. How can I check if any collider within my array gets triggered?
a better approach is to use OnCollisionEnter instead of OnTriggerEnter
You are missing the basics of Collision here. You don't need to reference the Colliders using an array.
Both OnCollisionEnter and OnTriggerEnter are called when the gameobject to which the script is attached collides with another collider. They return the collider with which it has collided.
So in your case its better to have a single collider on the parent gameobject rather than separate Colliders.
If you really want to have separate colliders for the airplane's body part then you need to add a collision checking script to each of the gameobject or you can add the collision checking script to your obstacles like building and have all your Airplane parts in a single tag.
Check out this guide on Unity collision

i need my player in unity to teleport to a certain location when he touches a tagged object

I need my player to teleport somewhere when he bumps into a (unity) gameobject with a certain tag (are you seen) on it. I already found a solution, but then I switched my player's movement to a character controller and now it doesn't work. This is my current script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class found: MonoBehaviour
{
private void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.tag == "are you seen")
{
transform.position = new Vector3(-15.38f, 1.93f, 62.1f);
}
}
}
When you switched to character controller, did you remove the rigidbody and collider? An OnTriggerEnter event still requires the player to have a collider with IsTrigger = true, and one of the gameobjects to have a rigidbody. Here is what the offical documentation says.
Note: Both GameObjects must contain a Collider component. One must have Collider.isTrigger enabled, and contain a Rigidbody. If both GameObjects have Collider.isTrigger enabled, no collision happens. The same applies when both GameObjects do not have a Rigidbody component.

How to teleport player to different scene via trigger?

I have used the most traditional method of doing this but it doesn't work. No response is initiated.
I've attached a rigidbody2d collider (with 0.0001 mass and no drag or gravity) to my player sprite and a box collider 2d with is trigger checked.
Sidenote: Outdoor1" is the name of the scene I want to teleport my player to.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class TeleportToScene : MonoBehaviour
{
[SerializeField] private string newScene;
void OnTriggerStay2D(Collider2D other)
{
if (other.CompareTag ("Player"))
{
SceneManager.LoadScene("Outdoor1");
I expect it to go over the box collider and change scenes but nothing happens. No error messages either.
to make OnTriggerStay2D you need to have an collider 2D set to trigger on the object who as the script TeleportToScene and a Rigidbody2D
So you scene will be, 2 object:
1.Player with
- Deplacement
- Collider2D(not trigger)
- Tag "Player"
2.Teleporter with
- TeleportToScene.cs
- Collider2D (trigger)
- Rigidbody2D (kinematic)
Hope that help !

Unity5 OnTriggerEnter2D not called after collision (2D game)

I'm brand new to Unity3D 5 and I'm having issues with my first 2D game involving collision detection. My moving object is a ball and has Rigidbody2D and CircleCollider2D. My stationary "collider or trigger" is a BoxCollider and has a script attached. The OnTriggerEnter2D should be fired when the ball passes through the stationary box. I've also tried OnCollisionEnter2D but I'm fairly certain I should use OnTriggerEnter2D because my stationary box is marked as a trigger.
My code:
public class LoseCollider : MonoBehaviour {
public LevelManager levelManager;
void OnCollisionEnter2D(Collision2D collision)
{
print("collide");
levelManager.LoadLevel("Lose");
}
void OnTriggerEnter2D(Collider2D trigger)
{
print("trigger");
levelManager.LoadLevel("Lose");
}
void OnCollisionEnter(Collision collision)
{
print("collide");
levelManager.LoadLevel("Lose");
}
void OnTriggerEnter(Collider trigger)
{
print("trigger");
levelManager.LoadLevel("Lose");
}
}
As you can see I'm testing all variations and none are being called. Here are my Unity properties for my objects:
And
I'm sure I'm missing something simple if anyone can please point it out to me.
If you are going to to use OnTriggerEnter2D, you must also use a 2D Collider. You are currently using BoxCollider as shown in your second screenshot.
Change that to BoxCollider2D and your Collision callback functions should be called.
I know you already solved it, but I ran into the same problem and found your post. I was using the correct 2D collider, I was missing the Ridgedbody 2D component. It took me a while to find that so I wanted to add it here in case someone else comes across the same problem.

On-collision script not working?

in advanced I would like to say if this is a really simple question with a simple answer, I apologize as I have just now gotten into programming.Basically, I'm trying to create a script that a block named blue(picture below) on collision with the FPSController, will get destroyed, here is my script:
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
void OnCollisionEnter (Collision col) {
if(col.gameObject.name == "Blue") {
Destroy(col.gameObject);
print ("collison detected");
}
}
}
for some reason, though, whenever the fps controller collides with the object known as "Blue" nothing happens, the print() function is not triggered nor is the destroy() function
Thank you in advaned ;)
Rigidbody` is missing from your Cubes.
1.Attach Rigidbody component to both cubes.
2.Also,set both Cubes Rigidbody to Is-kinematic. You must set both Cubes Rigidbody to Is-kinematic so that the Character Controller wont be able to move it. Note that if your cube is falling down after adding Rigidbody, simply disable Use Graivty on the Rigidbody.
Important:
3.Delete FPSController. Since you will be interacting with other Rigidbody GameObjects, use RigidBodyFPSController. It can be found in Assets\Standard Assets\Characters\FirstPersonCharacter\Prefabs. Drag RigidBodyFPSController to the Scene then attach Cube script to it.
You will notice there is a Rigidbody attached to RigidBodyFPSController. Don't modify the settings of it.
That's it. Everything should be working as expected.
Cube settings:
RigidBodyFPSController Settings:

Categories

Resources