i was making code for a project (i'm new in c#) and i'm so confused as to why my code dosent work
the code should make it so when a collider with the tag "floor" collides with my player it should change a variable,
no errors are coming up but the variable dose not change , here's the code where the problem is
void OnCollisionEnter2D()
{
if (GetComponent<Collider2D>().tag == "FLOOR")
{
jump = false;
Debug.Log("ITS A FLOOR");
}
}
Try this:
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "FLOOR")
{
jump = false;
Debug.Log("ITS A FLOOR");
}
}
My experience is mostly in 3D environments so I'm really not sure if I'm missing something, but your attempt to get a component doesn't make sense to me for two of reasons:
You didn't mention for which game object you would like the collider
The tag is found on the game object and not on the collider
Considering you want the variable jump to change. Debug with a breakpoint at the line
jump=false
Make sure the program reaches that line and make sure the scope of the variable is what you expect.
Going off the difference between "floor" in your description and then in your code, the string you are searching for may have different casing: "floor" != "FLOOR".
Try the following, if you need a case invariant string comparison:
if (string.Equals(GetComponent<Collider2D>().tag,"FLOOR", StringComparison.InvariantCultureIgnoreCase))
There is no void OnCollisionEnter2D() in Unity, only void OnCollisionEnter2D(Collision2D collision) where Collision2D collision is the collision that your object (player) entered.
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "FLOOR")
{
jump = false;
Debug.Log("ITS A FLOOR");
}
}
Aight, answers above already told the answer to the problem
void OnCollisionEnter2D()
{
if (GetComponent<Collider2D>().tag == "FLOOR")
{
jump = false;
Debug.Log("ITS A FLOOR");
}
}
Is this code attached to the player? Or to the floor? I assume it's attached to the player, in this case GetComponent() will try to get the Collider2D from the player's entity or whatever, but not from the collision
And you didn't declare the Collision2D parameter. Intellisense usually knows Unity's methods, might help you a lot
So, the right way to access tag from the collision would be
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "FLOOR")
{
//your code
}
}
Hope you'll get around
Related
So I am trying to make my first game using Unity and c#. I want my game to be a simple game of tag like I used to play when I was younger. I have tried using "OnCollisionEnter" and I was able to get that to change a counter that gave a bool a label. I realized that while this may work for tagging someone it does not help with other people tagging you. And tips on how I can make my code more like a "Tag Manager"?My current progress
you can have a Taggable.cs like this, not sure if this is even correct syntax, I don't care to actually write it out and test
class Taggable : MonoBehaviour {
bool it;
private void OnCollisionEnter(Collision collision) {
var other = collision.collider.GetComponent<Taggable>();
if(other != null) {
if(it) {
other.it = true;
it = false;
}else if(other.it) {
it = true;
other.it = false;
}
}
}
}
You could implement the logic such that your script checks to see if you are the tagger using Unity's tags property. Then, you could switch the tags of both the players, once you check if the collided object is a player.
Below is the code I suggest you use. I would personally use OnTriggerEnter to have more clearer logic between collider and player. Make sure to have a collider object and a Rigidbody on all players within the scene for the function to be called, and attach your script onto every player within the scene.
private void OnTriggerEnter (Collider col) {
// if you are the tagger
if (gameObject.tag == "Tagger") {
// and if the collided object is a regular player
if (col.tag == "Player") {
// the player is now a tagger
col.tag = "Tagger";
// depending on if you want to buildup taggers or switch them around,
// the below assignment would vary
gameObject.tag = "Player";
}
}
}
I begun the trek into Unity2D development a few months ago, so i'm still fairly new to all the unity engine jargon. In my game, I decided to implement the use of 'Lives'. I scripted out what (in my eyes) should work but every time the player dies instead of decrementing the lives counter and restarting it at the scene, it immediately loads the scene 1 (i.e. the GameOver screen) Is my logic here on this page incorrect or is there just a better way overall to handle lives than PlayerPrefs? (ALSO: Its worth mentioning that the Lives are instantiated/sent to playerprefs in the player script, I don't figure I need to include that one here)
This is the main block of code on my destroyer objects' script to account for 'death':
public static int lives;
void Start()
{
lives = PlayerPrefs.GetInt("lives");
}
void OnTriggerEnter2D(Collider2D other)
{
//If the trigger happens to be tagged as a 'Player', does this.
if (other.tag == "Player")
{
lives--;
if (lives < 0)
{
PlayerPrefs.DeleteAll();
SceneManager.LoadScene(1);
} else
{
SceneManager.LoadScene(0);
PlayerPrefs.SetInt("lives", lives);
}
}
if (other.gameObject.transform.parent)
{
Destroy(other.gameObject.transform.parent.gameObject);
}
else {
Destroy(other.gameObject);
}
}
As I mentinoned in comment, I think you forget to swithOn isTrigger field from Inspector which is under the Collider area.
Probably you thought like you are using onCollisionEnter. Also if you check the differences between OnTriggerEnter and OnCollisionEnter it will be very helpful.
As #Ugur Tufekci answers, But make sure to add BoxCollider2d not Box Collider.The simple box collider is 3d but your are detecting that onTriggerEnter2D that means 2d box collider has to be added
In Unity 5.6 C#, I know there is a way to check for if a collider is being touched by any other collider by using IsTouching.
However, I would like to know how to group two colliders together(that are touching each other), and how to check if they both are touching any collider besides each other.
I will give it a shot with the idea I mentioned in the comments (I see it is hard to understand with only the comments section).
I would use a list of collisions and store any touches here, filtering out the "partner" collider using OnCollisionEnter and OnCollisionExit.
Since both are attached to the same GameObject it is easy to filter them:
public class Collisions : MonoBehaviour
{
// Show in the Inspector for debug
[SerializeField] private List<Collider> colliderList = new List<Collider>();
public bool IsTouching => colliderList.Count != 0;
private void Awake ()
{
// Make sure list is empty at start
colliderList.Clear();
}
private void OnCollisionEnter(Collision collision)
{
// Filter out own collider
if(collision.gameObject == gameObject) return;
if(!colliderList.Contains(collision.collider) colliderList.Add(collision.collider);
}
private void OnCollisionExit(Collision collision)
{
// Filter out own collider
if(collision.gameObject == gameObject) return;
if(colliderList.Contains(collision.collider) colliderList.Remove(collision.collider);
}
}
Typed on smartphone but I hope the idea gets clear
I tried using OnCollisionExit, but it doesn't detect the other object's box collider 2D being disabled (When I disable the other objects box collider 2D it doesn't detect it as stopping being collided). I need the other objects collider to be disabled because I'm using it as a range indicator for punching and I don't want it to interact with other objects(example: The player pushes away the enemy with his range indicator). Is there another method I could use?
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.tag == "PunchRange")
{
Player.GetComponent<Fight>().PunchInRange = true;
}
if (collision.collider.tag == "KickRange")
{
Player.GetComponent<Fight>().KickInRange = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.collider.tag == "PunchRange")
{
Player.GetComponent<Fight>().PunchInRange = false;
}
if (collision.collider.tag == "KickRange")
{
Player.GetComponent<Fight>().KickInRange = false;
}
}
I want it to detect whenever it is not colliding with an object even when the object's box collider 2D is disabled.
You have to restructure your introduction as it is so convoluted idk what you exactly want.
To detect collision or its lack you have to use colliders(maybe put additional collider but as a trigger). However what I understood is that you want to use them as triggers. On Collider2D component there is value isTrigger that you can set. Setting it makes the collider still work but not phisically (other colliders can pass thru it). Set that on the collider and use:
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerExit2D.html
That should work for you.
Right now we work on a Infinity-Runner, and i got this weird bug. After something leaves the Screen it will be catched by a collider that destroys everything. And it works...almost. It will destroy the ground, Background stuff, enemys you jumped over but NOT the newly implemented "roadblocks".
Here is a picture how it looks like, the green placeholder is the roadblock-thingy
http://s4.postimg.org/8uaorv7ot/Bug.png
hope this might help in visualizing what im saying^^.
The Script i use for the Destroyed (the green box collider) is:
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Debug.Log ("Break is gonna happen");
Debug.Break();
}
else if (other.gameObject.transform.parent)
{
Destroy(other.gameObject.transform.parent.gameObject);
}
else
{
Destroy(other.gameObject);
}
}
and this is the script for the roadblocks:
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "Player")
{
Debug.Break ();
}
}
so basically nothing that would ever disrupt the DestroyerScript. Its even far more simple than the script on the enemys...but they get destroyed.
Thanks in advance for your help, i can provide more information if need.
(ohh, and all the art in this pic is placeholderstuff^^)
You can try to use OnTriggerExit2D to trigger when your player leave the collider. And makesure you added a suitable collider2d to your sprite before
http://docs.unity3d.com/ScriptReference/Collider2D.OnTriggerExit2D.html
I'm not use it before but I think it might works