Unable to destroy gameObject? - unity3d

I am trying to destroy a gameObject once it has collided with two other objects with the following code but it does not work.
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Sphere" && col.gameObject.tag == "Pyramid")
{
Destroy (GameObject.FindWithTag("Pentagon"));
}
}
Can someone offer a correction of my code because I can't seem to figure out how to destroy my "Pentagon"?

The problem is in your first line:
if (col.gameObject.tag == "Sphere" && col.gameObject.tag == "Pyramid")
You cant have 2 tags on a gameObject. I assume what you want to say is || instead of &&.
What are you trying to achieve in that second block of code? Instead of deleting the rigidbody, maybe just set RigidBody.Enabled = false.

Related

How do I call OnTriggerEnter multiple times in unity?

I have to push a button (inside a medical patient) multiple times, with OnCollisionEnter(); the problem is that it collides with the patient's mesh and the animations are always triggered, which is not good for me. With OnTriggerEnter(), it works fine as it is a trigger and does not collide with the medical patient's mesh, only with my avatar, which uses hand tracking. I want to call OnTriggerEnter() multiple times. I thought about instantiating the asset again, then destroying the first asset, but I have not gotten any luck. Please help! The following code is attached to my buttons inside the patient, which is to be palpated. How can I trigger OnTriggerEnter multiple times?
private void OnTriggerEnter(Collider other)
{
if (palpation_patient.animation_selection == 6 && palpation_patient.pain_level == 1)
{
palpation_patient.animator.SetLayerWeight(1, 1);
}
if (palpation_patient.animation_selection == 6 && palpation_patient.pain_level == 2)
{
palpation_patient.animator.SetLayerWeight(2, 1);
}
if (palpation_patient.animation_selection == 6 && palpation_patient.pain_level == 3)
{
palpation_patient.animator.SetLayerWeight(3, 1);
}
}

How to get my overlapsphere to work?

This is my error code :
Assets/Scripts/PlayerDamage.cs(166,35): warning CS0219: The variable `hitCol' is assigned but its value is never used
I'm just not sure why it's not working properly. I thought I was using it in the input function under attackVKC. I should mention this is for a multiplayer game in unity.
Basically damage is getting done to the player casting the attack and not to the other players. I'm trying to get an aoe (area of effect) attack.
private Collider[] hitColliders;
else if (Input.GetButton ("Special") && (Input.GetButtonDown ("Fire2") && offcd == true && offccd == true)) {
attackVKC();
StartCoroutine("GlobalCooldown");
StartCoroutine("GlobalCCooldown");
}
[Client]
void attackVKC(){
hitColliders = Physics.OverlapSphere (transform.position, special.rangeVKC, mask);
{
if (GetComponent<Collider>().tag == PLAYER_TAG)
{
CmdPlayerHit(GetComponent<Collider>().name, special.damageVKC);
}
}
Debug.LogError (GetComponent<Collider>().name);
}
I actually sloved this. I wasn't looping through the array and I didn't realise getcomponent was for the gameobject attached. So that's why it was hitting the player using the attack and not other players.

Compare tags in UnityScript

We are building a 3D game and right now I am stuck on an issue where I need to compare multiple tags to activate or deactivate a trigger that sends the player back to the respawn position.
This is currently the code:
#pragma strict
var spawnPoint : GameObject;
function OnTriggerStay ( other : Collider )
{
if (other.tag == "Player" && other.tag != "Ball")
{
other.tag == "Player";
other.gameObject.transform.position = spawnPoint.transform.position;
}
else if ( other.tag == "Ball" && other.tag == "Player" )
{
}
}
I am uncertain how to fix this to do the following:
If the player touches the trigger without there being a ball colliding with it, the player respawns. This is to create the feeling that the particles kill you.
If the player touches the trigger when there is a ball colliding with it as well, nothing happens to the player and so the player can freely pass through.
What we want to do is that we want to push a ball over a geyser so it covers it and if the geyser is not covered and the player tries to pass over it, the player respawns.
We also tried with another code and while it allows the ball to pass, it does not allow the player to do so. Even after the ball has been placed.
#pragma strict
var spawnPoint : GameObject;
function OnTriggerStay ( other : Collider )
{
if (other.tag == "Ball")
{
other.enabled = false;
}
else if (other.tag == "Player")
{
other.gameObject.transform.position = spawnPoint.transform.position;
}
}
So, I believe your problem is, when you use that function, you are only checking the first collision. So whats happening is your getting a value of either the player or the ball, not both. You need to store all the collision so you can compare all of them. To do that you can follow the generals of this documentation. http://docs.unity3d.com/ScriptReference/Collision-contacts.html
It's talking about collisions, but the same general principle should apply.
Hope this helps!

OnCollisionEnter is not working:

Please Guys help, I am new to Unity and programming:
I have a two Bouncing Ball, tag as BouncingBall1 and BouncingBall2, I want when a bullet hit both to destroy and if the time have not exceeded displaySecond and you have destroy the balls you win, but my problem is the OnCollisionEnter is not working. The part of wining is not working the rest are, my code fragment is below.
function OnCollisionEnter(col.collision) {
if ((displaySecond < 30) && (
col.gameObject.tag ==
BouncingBall1 == null &&
col.gameObject.tag ==
BouncingBall1 == null)) {
print("You have won");
}
}
col.gameObject.tag == BouncingBall1 == null
I think the problem is with the way you check if an object is destroyed. This isn't the way to do it. Also, you're checking if two identical lines of code are identical. So this will always return true if the display second is lower than 30.
Try changing it to
if ( (displaySecond < 30) &&
(col.gameObject.tag == "BouncingBall1") ) {
Debug.Log("You have won!")
}
It sounds like you want keep track of whether each ball has been destroyed and display "You have won" when both have been destroyed. This code is a little crude but should achieve this:
var ball1Destroyed = false;
var ball2Destroyed = false;
...
function OnCollisionEnter(col : Collision) {
if (col.gameObject.tag == "BouncingBall1") {
ball1Destroyed = true;
}
if (col.gameObject.tag == "BouncingBall2") {
ball2Destroyed = true;
}
if (displaySecond < 30 && ball1Destroyed && ball2Destroyed) {
Debug.Log("You have won");
}
}
This code assumes that the only collisions will be between bullets and balls (not between one ball and another), this can be done with collision layers.

Detect if third person camera touches cube unity3d

What I want is to destroy the cube when the third person camera touches it...but anything i have tried so far fails...
Here is the code i have tried:
#pragma strict
var other : GameObject;
function Start () {
}
function Update () {
}
function OnCollisionEnter ( collision : Collision) {
if (collision.tag == "Character")
Destroy (collision.gameObject);
}
Thanks for any suggestions!
There are two simple ways to do this. One of them is attaching a script to the character to destroy specified objects and the other one is to attach a script to the object to be destroyed on inpact with the character, but on both of those ways you NEED the Rigidbody component to be attached too.
Adding this to the object to be destroyed and tagging the character:
[RequireComponent (typeof (Rigidbody))]
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Character")
Destroy(this.gameObject);
}
OR
Adding this to the character and tagging the objects to be destroyed:
[RequireComponent (typeof (Rigidbody))]
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "ToBeDestroyed")
Destroy(col.gameObject);
}
remember: this code is in C#, you`ll need to convert to javascript if you are going to add to an existing script