i want to instantiate a prefab when a prefab collides with another instantiated prefab object.
i am trying using this code, this code works with normal object but not with instantiated object.
private IEnumerator OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "table" || collision.gameObject.name == "Resources/Prefabs/Cube")
{
yield return new WaitForSeconds(1f);
// Destroy(gameObject);
spawnCube = Instantiate(Resources.Load("Prefabs/Cube"), new Vector3(288, 4, 208), Quaternion.identity) as GameObject;
}
}
}
You are using gameobject name to compare collision collision.gameObject.name == “table” . When the gameobject is in hierarchy it will be “table”, but when you instantiate the prefab table becomes “table(Clone)” in hierarchy. So you have to compare with table name as collision.gameObject.name == table(Clone) or you have to compare it with the tag and set the tag for prefab in the inspector and use gameObject.tag == “table” to compare. @wasimsahi
@Ashokkumar-M@wasimsahi
hey, I am facing the same kind of issue, but I am comparing the tag and not the name.
basically I have a prefab with multiple child objects and I am trying to detect the collision between one prefab’s child object with another prefab’s child object.
I have my script & Rigidbody attached to the parent and each child objects has a box collider.
here the “doors” are my child objects. and each element of the “doors” has a “Door” Tag. please help!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Door : MonoBehaviour
{
public GameObject[] doors;
private void Start()
{
int rand = Random.Range(0, doors.Length);
doors[rand].tag = "Closed";
}
public void OnTriggerStay(Collider other)
{
if (other.CompareTag("Player"))
{
Debug.Log("we collided");
enableDoorOpening();
}
}
public void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag("Door"))
{
Debug.Log("we collided with door");
}
}
public void enableDoorOpening()
{
Debug.Log("InsideDoorOpening");
if(Input.GetKey(KeyCode.Keypad1))
{
if (doors[0].CompareTag("Closed")) { return; }
doors[0].transform.localRotation = Quaternion.Slerp(doors[0].transform.rotation, Quaternion.Euler(0, 65f, 0), 1f);
}
if (Input.GetKey(KeyCode.Keypad2))
{
if (doors[1].CompareTag("Closed")) { return; }
doors[1].transform.localRotation = Quaternion.Slerp(doors[1].transform.rotation, Quaternion.Euler(0, 365f, 0), 1f);
}
if (Input.GetKey(KeyCode.Keypad3))
{
if (doors[2].CompareTag("Closed")) { return; }
doors[2].transform.localRotation = Quaternion.Slerp(doors[2].transform.rotation, Quaternion.Euler(0, 305f, 0), 1f);
}
if (Input.GetKey(KeyCode.Keypad4))
{
if (doors[3].CompareTag("Closed")) { return; }
doors[3].transform.localRotation = Quaternion.Slerp(doors[3].transform.rotation, Quaternion.Euler(0, 245f, 0), 1f);
}
if (Input.GetKey(KeyCode.Keypad5))
{
if (doors[4].CompareTag("Closed")) { return; }
doors[4].transform.localRotation = Quaternion.Slerp(doors[4].transform.rotation, Quaternion.Euler(0, 185f, 0), 1f);
}
if (Input.GetKey(KeyCode.Keypad6))
{
if (doors[5].CompareTag("Closed")) { return; }
doors[5].transform.localRotation = Quaternion.Slerp(doors[5].transform.rotation, Quaternion.Euler(0, 125f, 0), 1f);
}
}
//public void OnTriggerEnter(Collider other)
//{
// if (other.CompareTag("Door"))
// {
// Debug.Log("we collided with door");
// }
//}
}