Collider2D trigger on a different gameObject C# Unity3D not working - c#

In Unity, how can my code detect when a trigger occurs with a specific distinct object?
I already tried several things, such as:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public GameObject DualCannon_PU;
void OnTriggerEnter2D (Collider2D coll) {
if (coll.name == "DualCannon_PU") {
Debug.Log ("DualCannon PowerUp");
}
}
}
But it doesn't work: it doesn't trigger anything and "DualCannon PowerUp" does not appear in console.
In player gameObject "is trigger" is checked and my powerUp (DualCannon_PU) "is trigger" is not checked.
I noticed that I had 2 "Player Controller (Scripts)", I deleted the first one but the problem still persists...
an example of what I want:
I have 4 gameObjects and both have 2D Colliders
A. Player ship ("is trigger" is on)
B. Enemy projectiles
C. Health PowerUp
D. Dual Cannon PowerUp
Condition I want:
A is triggered by B, C, and D
When B triggers with A in A script (Player.cs) executes: health -= laser.GetDamage();
When C triggers with A in A script (Player.cs) executes: health = health + 10;
When D triggers with A in A script (Player.cs) executes isDualCannon = true

first you need to:
Put the "Is trigger" On
Make sure you're colliders are in the border of the gameObject(edit Colliders and drag until the green lines are in the corners)
If the error still consist comment on the answer and I will see what I can do
It works fine with mine:
This guy will hit
this guy
using this code:
void OnTriggerEnter2D(Collider2D col){
Debug.Log ("notset = " + col.name);
}

Related

My problem is that my player is not attaching to the roof when he jumps

My problem is that my player(box) is not attaching itself when it jumps and reaches the roof(really long rectangle). There is a ground(really long rectangle) and a roof and the player is in between them with just enough room to jump that he reaches the roof. There are two box colliders and one of them is trigger and the collision field is slightly expanded.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Platform_Attach_Script : MonoBehaviour
{
public GameObject Player;
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
Player.transform.parent = transform;
rb.mass = 0;
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
Player.transform.parent = null;
}
}
}
This code has worked for previous projects I have worked but I have been tweaking it for my current project. I have tried changing the mass for when my player jumps up and reaches the box collider with the trigger but then I realised I am just changing the mass of the roof so that's why it fell a few times then I tried looking on Youtube and forums for how to call a Gameobject's function from a particular script I couldn't find anything. So then I thought I was trying to do the impossible, as I've been coding for 6 months now.
So the bottom line is when my player jumps it's meant to get attached to the roof and when it jumps again it's meant to jump back on the ground and there is a script that keeps it moving constantly.

Button switching color in unity

I am very new to unity and coding in general, I am trying to create a button in unity that eventually opens a door in my game, but I immediatle ran into a problem, when I collide with the button it doesnt switch color, Im not sure if something is wrong with my code or my settings in unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Button : MonoBehaviour
{
[SerializeField]
GameObject switchOn;
[SerializeField]
GameObject switchOff;
public bool isOn = false;
void Start ()
{
gameObject.GetComponent<SpriteRenderer>().sprite = switchOff.GetComponent<SpriteRenderer>().sprite;
}
void OnTriggerEnter2D(Collider2D col)
{
gameObject.GetComponent<SpriteRenderer>().sprite = switchOn.GetComponent<SpriteRenderer>().sprite;
isOn = true;
}
}
I have 2 different button textures put on the on and off gameObjects a green one and a red one, does anyone know if something is wrong with the code
debug a line in your on collision enter, likely its not getting called. Make sure your player has a rigidbody and a collider and make sure your door has a collider. Also make sure trigger is checked on your player collider.

OnCollisionEnter somehow not working [duplicate]

i am bloody beginner with Unity and i am currently working on a 2D Brawler. The movement works perfectly but my colliders don't do what they should... I want to detect if two GameObjects Collide (Spear and Player2) and if the collide Player2s healthPoints should decrease by Spears AttackDamage.
The names of the GameObjects are also their tags. The Spears Prefab has following configuration: SpriteRendered(Material Sprites-Default), BoxCollider2D(Material None Physics Material 2D, IsTrigger(not activated), UsedByEffector(also not activated) Rigidbody2D(Kinematic, None Material, Simulated(Activated), KinematicContacts(activated), Standard configs for the rest))
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpearCtr : MonoBehaviour {
public Vector2 speed;
public float delay;
Rigidbody2D rb;
void Start ()
{
rb = GetComponent<Rigidbody2D>();
rb.velocity = speed;
Destroy(gameObject, delay);
}
void Update ()
{
rb.velocity = speed;
}
}
The Players Configurations
The Spears Configurations
This was the code i have tried before
OnCollision2D(Collision2D target);
{
if (target.gameObject.tag == "Spear")
{
hp = -1;
if (hp <= 0)
{
alive = false;
}
}
}
I hope someone can tell me how to get this working
Thanks for all the answers
(BTW sorry for my bad english I am austrian)
enter image description here
enter image description here
Reasons why OnCollisionEnter() does not work:
Collison:
1.Rigidbody or Rigidbody2D is not attached.
At-least, one of the two GameObjects must have Rigidbody attached to it if it is a 3D GameObject. Rigidbody2D should be attached if it is a 2D GameObject/2D Collider.
2.Incorrect Spelling
You failed to spell it right. Its spelling is also case sensitive.
The correct Spellings:
For 3D MeshRenderer/Collider:
OnCollisionEnter
OnCollisionStay
OnCollisionExit
For 2D SpriteRenderer/Collider2D:
OnCollisionEnter2D
OnCollisionStay2D
OnCollisionExit2D
3.Collider has IsTrigger checked. Uncheck this for the OnCollisionXXX functions to be called.
4.The script is not attached to any of the Colliding GameObjects. Attach the script to the GameObject.
5.You provided the wrong parameter to the callback functions.
For 3D MeshRenderer/Collider:
The parameter is Collision not Collider.
It is:
void OnCollisionEnter(Collision collision) {}
not
void OnCollisionEnter(Collider collision) {}
For 2D SpriteRenderer/Collider2D:
6.Both Rigidbody that collides has a isKinematic enabled. The callback function will not be called in this case.
This is the complete collison table:

Why the script is not working now as before?

This is the top of the script:
using UnityEngine;
using System.Collections;
using System.Reflection;
public class DetectPlayer : MonoBehaviour {
GameObject target;
int counter = 0;
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Platform")
{
Debug.Log("Touching Platform");
}
}
I'm using a debugger and a break point and it does getting to the line
if (collision.gameObject.name == "Platform")
And on the property name of the gameObject i see: "ThirdPersonController" but it's never get in to the Debug.Log
The script is attached to the Platform like it show in the screenshot. I'm running the game move the player to the Platform when it touch it it stop on the break point but never enter to the Debug.Log
collision.gameObject.name will be called on the gameobject which your collider is attached to so if your collider is not attached to the game object which is named platform , then it wouldn`t be called.
I think #MiladQasemi is right, but I'll try explaining it another way.
The problem as I see it, is your script is attached to your Platform object, and therefore the code if (collision.gameObject.name == "Platform") will never be true. Because the script and the platform are one, an object cannot collide with itself.
Change the code to be:
if (collision.gameObject.name == "ThirdPersonController")

Object Shoots On x-z Axis

I actually have 2 issues. The first problem I have is that when I start my game all 4 cannons do their animation sequence for no reason, nothing happens. I'd like this to not happen. The second problem I have is the cannon ball that shoots out spawns on the floor and flies along the floor. Here is the code for the firing sequence:
using UnityEngine;
using System.Collections;
public class Cannon : MonoBehaviour {
public AudioClip sound;
public GameObject prefab;
public GameObject ejectPoint;
void Start () {
prefab = Resources.Load ("Cannon_Ball") as GameObject;
}
public void Fire () {
GameObject Cannon_Ball = Instantiate (prefab) as GameObject;
Cannon_Ball.transform.position = transform.position + ejectPoint.transform.forward * 2;
Rigidbody rd = Cannon_Ball.GetComponent<Rigidbody> ();
rd.velocity = ejectPoint.transform.forward * 130;
AudioSource.PlayClipAtPoint(sound, transform.position, 1);
GetComponent<Animation> ().Play ();
}
}
Here is a GIF of the problem:
fix 1: Uncheck 'Play Automatically' or find equal option in your Animator. I think after you start game, your Animator change his state from "Start" to "Shoot" because of bad conditions setting or even totally lake of conditions . If you paste here screenshot of your animator, that would be useful.
If you want to fix your second problem you should watch this video:
BRICK SHOOTER - Official Unity Tutorial

Categories