Why is my object not find other Gameobject name? - unity3d

I want to destroy my object when it will enter in OnTriggerEnter method.
I am using something similar this code
void OnTriggerEnter(Collider C)
{
if(C.name == "GameObject Name")
{
Destroy(gameObject);
Time.timeScale=0;
}
}
my object enter into OntriggerEnter event but it can't find C.name == "GameObject Name" .
Sorry friends, now one problem is occured..actually my object is not detect collider gameobject.I mean my object is not enter in trigger event. .now What can I do?

Or you can use Tag option on the object. Right corner of the Unity choose Tag 'Player' From Inspector and apply this code[C#].
void OnTriggerEnter(Collider collision){
if (collision.gameObject.tag == "Player") {
Destroy(Object_name_to_destroy);
Time.timeScale=0;
}
}

You want the name of the GameObject belonging to the Collider, so use C.gameObject.name.

Sorry this is super late but for someone facing this issue in future...try checking for blank spaces after your game object name i.e "example_name " is different from "example_name"

Related

how do i make sword work with enemy collider

here is my current code:
public GameObject enemy;
void OnCollisionEnter(UnityEngine.Collision collisionInfo)
{
if (collisionInfo.collider.tag == "sword")
{
Debug.Log("works");
enemy.SetActive(false);
}
else
{
Debug.Log("doesnt work");
}
}
i have attached this to the enemy and also i tried a different script attached to the sword
void OnTriggerStay(Collider col)
{
if (Input.GetButtonDown("Fire1"))
{
if (col.GetComponent<Collider>().tag == "enemy")
{
Destroy(col.gameObject);
}
}
}
both codes don't work, it seems that the problem isnt with the sword collision cus i have also added the tag to another gameobject and it doesnt work. i looked online but havent found anything that works so fat
update: it seems that i have forgot you need a rigid body for collision detection. simple mistake but it made my code not work!
For collision enter to work, both the sword and the enemy colliders need to have the "IsTrigger" box unticked.
For on triggerstay is the opposite, both need to have that box ticked.
Basically, Triggers do not collide they call that OnTriggerEnter function, while non-triggers use the OnCollisionEnter.

Can't detect collision with child and ground

I have the below script attached to the ground game object to detect collisions from my players' child objects but for some reason, the collisions are not detecting.
My player (parent, empty game object) has a Rigidbody and Jump script attached to it, meanwhile, the child game objects (body & feet) have just box colliders on them.
Would love to know why this isn't working :)
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Body")
{
Debug.Log("Game over!");
}
else if (collision.gameObject.tag == "Feet")
{
Debug.Log("Alive!");
}
}
For some reason, it works when you do collision.collider.tag instead of collision.gameobject.tag, like so:
if(collision.collider.tag == "Head")
{
Debug.Log("Game over!");
}
Not exactly sure why this works, but it works! Defo going to look into this a bit more.

Why is "Colls" an empty Array?

I try to judge a player's approach to a monster using "OverlapSphere."
But somehow it doesn't work the way I want it to.
Is there anything wrong with the code below?
bool IsPlayerNear()
{
Collider[] colls = Physics.OverlapSphere(transform.position, 3f);
foreach(Collider hit in colls)
{
if(hit.gameObject.tag == "Player")
return true;
}
return false;
}
The player character has a capsule collider and "Player" tag.
But the "colls" array contains nothing.
A frequent cause of this sort of problem is that the capsule collider is attached to a child or parent of the tagged gameobject, so maybe check that, your code looks OK to me.

How to access the Parent Object when i identified its Child Object in Unity

I dont know how to access the Parent Object when i know its Child Object
Ive setup joints on a rope that contain colliders but not on the actual parent gameobject.
i put a tag on the first joint (child Object) and would like to destroy the entire rope on collision.
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Player")
{
Debug.Log("Destroy Rope");
Debug.Log("Joint Name" + col.gameObject.name);
}
}
Anyone help this noobie question?
You can use childObject.transform.parent.gameObject for this

[Unity][Javascript] Why are these simple js scripts not working in Unity?

I've been sort of teaching myself and sort of learning from Jimmy Vegas on youtube: https://www.youtube.com/channel/UCRMXHQ2rJ9_0CHS7mhL7erg
If you haven't seen those tutorials or don't want to look, one of the things he does is create a small script that destroys a coin when the player collider hits it, but mine isn't working. Code below (a little mis-formatted, sorry, couldn't get it to format correctly):
function OnCollisionEnter (collision : Collision) {
if(collision.gameObject.tag == "coinCollect") {
Destroy(this.gameObject);
}
}
I applied the script to a prefab and placed a bunch of coins around a little area, additionally, I made a capsule collider in a first person controller tagged "coinCollect", and ticked "Is Trigger"
Also, I'm trying to make a teleporter that teleports the first person character from one teleporter to another. Code below:
var warptarget001 : GameObject;
var warptarget002 : GameObject;
function OnTriggerEnter (col : Collider) {
if (col.gameObject.tag == "warp001") {
this.transform.position = warptarget002.position;
} else if (col.gameObject.tag == "warp002") {
this.transform.position = warptarget001.position;
}
}
I have four objects here, two warp pads and two warp targets. The two warp pads are tagged "warp001" and "warp002", respectively and the two warp targets are not assigned anything in the code, but assigned by dragging and dropping an empty object into the Serialized Field the script provides. Both pads have capsule colliders with "Is Trigger" unticked but it doesn't work either way, ticked or unticked.
Can anyone tell me what I might be doing wrong? Thank you.
The script was all correct, my problem was that my parent "FPSController" object didn't have a Rigidbody applied to it and should be the only object (as opposed to the "FirstPersonCharacter" object I had nested inside of it) that the scripts are applied to. That seemed to fix the problem.
The correct code is:
/* coincollect.cs */
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class coincollect : MonoBehaviour {
private int _score;
[SerializeField]
private Text _text;
void OnTriggerEnter ( Collider collision ){
if(collision.gameObject.tag == "coin"){
Destroy(collision.gameObject);
_score++;
_text.text = "Score: " + _score;
}
}
}
and:
/* warp.js */
var warptarget001 : GameObject;
var warptarget002 : GameObject;
function OnTriggerEnter (col : Collider) {
if (col.gameObject.tag == "warp001") {
this.transform.position = warptarget002.transform.position;
}
if (col.gameObject.tag == "warp002") {
this.transform.position = warptarget001.transform.position;
}
}
The only thing I can think of for your first problem is that it shouldn't need IsTrigger ticked. Other than that, it sounds like it should work (unless I'm missing something).
For the second problem you're having (with the warps), I don't think you can use warptarget001 by dragging and dropping objects into the fields. The reason being that what you've dragged into that field isn't the same object instance that's inworld.
You should assign their values through the code (preferably in the Start method), by using GameObject.Find("name") for example. This way, warptarget001 corresponds to the inworld gameobject.