Why the script is not working now as before? - c#

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")

Related

C# Unity not displaying Debug.Log() console

I am writing a script that shows a collision between one object and an obstacle. The code runs but does not output within the console. Is there something that I am doing wrong?
using UnityEngine;
public class collision : MonoBehaviour{
void OnConllisionEnter (Collision collisionInfo)
{
if(collisionInfo.collider.tag == "Obstacle")
{
Debug.Log("We Hit an obstacle!");
}
}
}
I added a tag to the object as I will be adding more obstacles to simplify the process. I checked for semicolons and any other errors that would stand out to me. I am not sure what I am supposed to change or if I am missing something.
You actually have a typo in the methods name
// Here
// |
// v
void OnConllisionEnter (Collision collisionInfo)
The code should be
using UnityEngine;
public class collision : MonoBehaviour{
void OnCollisionEnter (Collision collisionInfo)
{
if(collisionInfo.collider.tag == "Obstacle")
{
Debug.Log("We Hit an obstacle!");
}
}
}
When using OnConllisionEnter, check your collider component to make sure the [Is Trigger] flag isn't checked
Collider Component, Is Trigger Flag
Also, remember that both objects have to have a collider component attached to them and only one of them will need a rigid body component attached to it (both of which have to match the correct world space, i.e. 2D/3D RigidBody and Collider.
More about Collisions:
https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

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.

Why my player is invisible, when he dies?

I'm beginner in Unity. I'm making a game in Unity 2D. In this game I have a player, platforms and a die_zone, but when my player die his sprite becomes invisible. How can I change my script to make the sprite visible?
Here is the script:
using UnityEngine;
public class Death : MonoBehaviour
{
public GameObject Respawn;
public Sprite Player_;
public GameObject Player;
public GameObject die_zone1;
public GameObject die_zone2;
public GameObject die_zone3;
public GameObject die_zone4;
public void die()
{
Player.transform.position = Respawn.transform.position;
Player.GetComponent<SpriteRenderer>().sprite = Player_;
}
void OnTriggerEnter2D(Collider2D deth)
{
if (die_zone1.tag == "Death" || die_zone2.tag == "Death" || die_zone3.tag == "Death" || die_zone4.tag == "Death")
{
die();
}
}
}
I will glad to any suggestion.
Thanks!
Let's break your code down to walk through its points of failure.
using UnityEngine;
public class Death : MonoBehaviour
{
public GameObject Respawn;
public Sprite Player_;
public GameObject Player;
public GameObject die_zone1;
public GameObject die_zone2;
public GameObject die_zone3;
public GameObject die_zone4;
Type/field declarations, probably not your problem
public void die()
{
Player.transform.position = Respawn.transform.position;
Player.GetComponent<SpriteRenderer>().sprite = Player_;
}
Suspect, as your bug happens when you die. But maybe there's other code executing?
void OnTriggerEnter2D(Collider2D deth)
{
if (die_zone1.tag == "Death" || die_zone2.tag == "Death" || die_zone3.tag == "Death" || die_zone4.tag == "Death")
{
die();
}
}
}
Ah, looks safe enough. There's a conditional invocation to die(). Presumably your code could crash in the conditional (diezonetag == ...), but that would abort further execution without having edited the Player object. So probably okay.
Alright, so that leaves us with die(). There are two lines, each is a point of failure. They seem to be independent changes of each other (move the player, change their sprite) so perhaps you can test each of them in isolation to determine your culprit. You could do this by commenting out one, running, and seeing if your game still breaks. If that works, then comment out the other (and uncomment the first), run, and hopefully your game breaks.
Player.transform.position = Respawn.transform.position;
Hm, so what is the value of Respawn.transform.position? If that's off-screen or invalid, one would suspect that'd make your player position offscreen or invalid.
You can debug this one by looking at your scene view before/after your Player has died. What's their position before / after?
Player.GetComponent<SpriteRenderer>().sprite = Player_;
What's Player_ set to? Is it, perhaps, null? Alternatively, if you changed your player's sprite to be its death-state sprite in-editor, do you see anything? Maybe your death sprite is fully transparent, or the combination of your death sprite and your spriterenderer's settings makes it not appear on your screen?
When you click on your player object, does the sprite field appear in the inspector, and is it set to something sane?
Actually, you're setting Sprite to a GameObject. How's that supposed to work? Are you setting Sprite to a GameObject containing your sprite, instead of a Sprite itself? This doesn't seem like it should compile. According to Unity's documentation (which I got to by googling unity spriterenderer sprite) here https://docs.unity3d.com/ScriptReference/SpriteRenderer.html the type of SpriteRenderer.sprite is a Sprite, whereas you have a GameObject. Something seems wrong.
Remove this line, or check the sprite of Player_. Also double check if he is invisible or just moved back to the spawn point.
Player.GetComponent<SpriteRenderer>().sprite = Player_;

Enabling/Disabling Script in C# failing to work

So I've been developing a 2D platformer game in C# and Unity and as a part of the game I have been developing powerups. One of these in invincibility, so when the player collides with the game object they cannot be killed for a period of time. I am relatively new to Unity and C# and read that I can use '.enabled' to enable/disable an external script that is attached to the same object. However, when I activate the powerup the object is destroyed but if I collide with an enemy or object I still die. Can anyone see why this is happening.
Below is the script that I have developed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InvincibilityPowerup : MonoBehaviour
{
public int Duration = 15;
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.gameObject.tag == "Shield")
{
Destroy(GameObject.Find("Invincibility"));
StartCoroutine("Invincible");
}
}
IEnumerator Invincible()
{
Collision pIn = gameObject.GetComponent<Collision>();
pIn.enabled = false;
yield return new WaitForSeconds(Duration);
pIn.enabled = true;
}
}
1) GameObject.Find is completely unnecessary here. You already know which object is the invincibility powerup: its the one this script is attached to
2) Collision pIn = gameObject.GetComponent<Collision>(); both a) doesn't do what you want it to (you want to get the OTHER game object!) b) doesn't work anyway (Collision is not a component, Collider is)
3) you're destroying this before starting the coroutine, meaning your coroutine is being destroyed too.

Collider2D trigger on a different gameObject C# Unity3D not working

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);
}

Categories