Why my GameObject named "pipo" is not destroyed
This is my script:
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "pipo")
{
Destroy(other.gameObject.transform.parent.gameObject);
}
}
Try to change your Code a little bit, first you should generally use CompareTag() which gives Error Messages when the given Tag doesn't exist.
After that you can add a check to see if the gameobject has a parent and depending on that destroy its parent or itself.
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("pipo")){
return;
}
if(other.gameObject.transform.parent) {
Destroy (other.gameObject.transform.parent.gameObject);
}
else {
Destroy ( other.gameObject);
}
}
When the object still doesn't get destroyed, you need to make sure that:
The Gameobject you want to destroy has the tag called "pipo"
The GameObject where this script lies has IsTrigger enabled as well as a Collider
The "pip" GameObject has a Colllider and is not set to IsTrigger
Both objects have a Rigidbody component attached to them
Collider is attached to the "pipo" GameObject and not its parent
Related
I am trying to make a check point, I want to make it so when the character hits the checkpoint, the collider turns off (I am open to using a raycast but that wasn't the current plan). I am still new to unity and I can't get the code to compile. I think my issue may be just not calling objects properly???
public class checkpoint : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
cp = GetComponent<Collider>();
cp.isTrigger = true;
object = GameObject.Find("Check Point");
}
private void OnTriggerEnter(cp)
{
cp.GetComponent(BoxCollider).isTrigger = false;
}
}
Not sure what is wrong
When you look at the code in your IDE, you will see red outlines underneath the exact points in your code that have issues. When you hover your cursor over these points the IDE will tell you what the issue is.
You can even use the Show potential fixes functionality and the IDE can often fix the issue for you automatically.
Here is the code modified so that it compiles.
public class checkpoint : MonoBehaviour
{
Collider cp;
// Start is called before the first frame update
void Start()
{
cp = GetComponent<Collider>();
cp.isTrigger = true;
GameObject gameObject = GameObject.Find("Check Point");
}
private void OnTriggerEnter(Collider other)
{
cp.isTrigger = false;
}
}
You were missing declarations for your cp and object variables. Additionally object is a reserved keyword in C#, so you can't use it as a variable name.
On the OnTriggerEnter function declaration the cp parameter was missing its type Collider.
In the body of the method the GetComponent call has incorrect syntax.
UPDATE: You can use the Destroy method to destroy the Collider component when something enters inside its bounds for the first time.
[RequireComponent(typeof(Collider))]
public class Checkpoint : MonoBehaviour
{
void Reset()
{
var collider = GetComponent<Collider>();
collider.isTrigger = true;
}
private void OnTriggerEnter(Collider other)
{
var collider = GetComponent<Collider>();
Destroy(collider);
}
}
If you want to destroy the whole GameObject that contains the checkpoint component and the Collider component you can use Destroy(gameObject);.
I have the code for destroying a Cube GameObject when it collides with the Terrain. However, Im not sure how I would then after instantiate a New Sphere GameObject in its place after the cube is destroyed.
This is the current code:
{
void OnCollisionEnter(Collision collision)
{
if (collision.collider.gameObject.tag != "Destroy")
{
Destroy (gameObject);
}
}
}
1) Attach this script to your terrain game object and not the cube.
2) Add a new tag in the editor for cube objects (e.g cube).
3) Create a new sphere prefab instance that you can access through the script containing the OnCollisionEnter() event.
void OnCollisionEnter(Collision collision)
{
if (collision.collider.gameObject.tag == "Cube")
{
//store the transform component of the gameobject to be destroyed.
var transf = collision.gameObject.transform;
//Destroy the collided gameobject
DestroyImmediate(gameObject);
//Instantiate in the position and rotation of the destroyed object.
Instantiate(sphere, transf.position, transf.rotation);
}
}
I want to know which gameobject is clicked with mouse on a 2D project
I used
void Update()
{
if (Input.GetMouseButtonDown(0))
{
clickTime = DateTime.Now;
mousePosition = Input.mousePosition;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
if (hit != null && hit.collider != null)
{
}
}
}
but it never goes in the second if condition
EDIT: I am working on a single script and access all gameobject from there using GameObject.FindGameObjectWithTag() and as I understand thats why the collider code in main script doesnt triggered.
I added a screenshot my code is in GameObject
This method works perfect on both desktop and mobile apps:
Add a collider component to each object you want to detect its click event.
Add a script to your project (let's name it MyObject.cs).
This script must implement the IPointerDownHandler interface and its method. And this script must add the Physics2DRaycaster to the camera. The whole body of this script could be like this:
public class MyObject : MonoBehaviour, IPointerDownHandler
{
private void Start()
{
AddPhysics2DRaycaster();
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
}
private void AddPhysics2DRaycaster()
{
Physics2DRaycaster physicsRaycaster = FindObjectOfType<Physics2DRaycaster>();
if (physicsRaycaster == null)
{
Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
}
}
}
Add the MyObject.cs script to every object you want to detect click on it. After that, when you click on those objects, their name will display on Console.
Make sure that EventSystem exists in your project's Hierarchy. If not, add it by right click.
P.S. The MyObject.cs script now has IPointerDownHandler. This detects click as soon as you touch the objects. You also can use IPointerUpHandler and IDragHandler for different purposes!
and sorry for my bad English.
When you use a raycast you should set a collider on your sprite
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
if (hit.collider != null) {
Debug.Log ("CLICKED " + hit.collider.name);
}
}
}
This is working for me in unity 5.6
Note: "LeftClick" is just a "GameObject" nothing else, I called it like this for better identification :)
EDITED
I test this method for the UI button but it's not working; so I used a different approach. For the UI button you can add a listener like this:
GameObject.Find ("YourButtonName").GetComponent<Button> ().onClick.AddListener (() => {
});
Consider using OnMouseOver() method (docs):
private void OnMouseOver() {
if (!Input.GetMouseButtonDown(0)) return;
// Your logic here.
}
This method only works with collider attached to gameobject, just like Raycast.
It would be cleaner and slightly more performant because there are no multiple Update() methods to iterate each frame. Something to keep in mind if you have dozens or hundreds of clickable objects.
(Although there are reports that this method doesn't work with multiple cameras one of which is ortographic one, in which case it is not an option for you.)
In World Scale AR, I want to move to an object I placed using SpawnOnMap. When I touch that object, I want that object to be erased. I made the Player have a script called PlayerController. I made a tag for the object so that when it touches, it should destroy or set it active. When I walk towards the object, nothing happens. I've tried OnTriggerEnter and OnCollisionEnter already. Is there some kind of method I'm missing that Mapbox provides?
public class PelletCollector : MonoBehaviour
{
public int count = 0;
// Start is called before the first frame update
void Start()
{
}
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Pellet")
{
count++;
Debug.Log("Collected Pellets: " + count);
other.gameObject.SetActive(false);
//Destroy(other.gameObject);
}
}
}
So I have a sprite and it's name is "princess" and when a cube touches my sprite
I want the princess to be destroyed. I added a rigidbody, and a box collider, but for some reason the cube just goes through the princess sprite.
The cube is generated with code so it's name is "Cube" according to the hierachy so I wrote this code
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.name == "Cube")
{
Destroy(this.gameObject);
}
}
I think Destroy(this.gameObject) would destroy the princess, but they're not even colliding.
Any ideas?
Here is what the "game" looks like.
Game
Check if you checked the "is trigger" inside the box colliders.
You should use tag because if you spawn multiple cubes the will have names like "Cube (1)" and so on.
You need to use collider2d
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Cube")
{
Destroy(this.gameObject);
}
}
both of your object needs to have box collider2d and rigidbody2d and not set to "is trigger". example:
Destroy(this.gameObject) is working for own object where
collision.gameObject.setActive("False") is working for collide object
This code is working on my game. where "Player" is my Sprite tag. And make sure 'Is Trigger' is checked for your Cube
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Player")
{
Destroy(gameObject);
}
}
OR alternatively in your case below code you could use in Cube script then might be work
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "princess")
{
collision.gameObject.setActive("False")
}
}