Instantiated Prefab not firing OnTriggerEnter?
Hello.
I drag a prefab of my bullet, and it makes my barrel explode, as everything is intended. I then use the shoot button to instantiate a bullet, and the bullet and barrel ignore each other? I have a Debug.Log that works with the dragged in bullet, but not the instantiated one. Any idea what I could be doing wrong? Thanks.
Barrel Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BarrelScript : MonoBehaviour
{
GameObject Barrel;
// Start is called before the first frame update
void Start()
{
Barrel = GameObject.Find("barrel");
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.name =="Bullet")
{
Debug.Log("Hit!");
GameObject Explosion = Instantiate(Resources.Load("FireExplosion", typeof(GameObject))) as GameObject;
Explosion.transform.position = Barrel.transform.position;
Barrel.SetActive(false);
}
}
}
Player Script w/Gun
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerScript : MonoBehaviour
{
private Animator Anim;
private GameObject GoreHolder;
private string ShootingParameter = "IsShooting";
private GameObject Bullet;
private bool isDead;
private bool isTossing = false;
void Awake()
{
Anim = GetComponent<Animator>();
GoreHolder = GameObject.Find("GoreHolder");
GoreHolder.SetActive(false);
Anim.Play("Pistol_Idle");
isDead = false;
}
void Update()
{
if (Input.GetKeyDown("space"))
{
Shooting();
}
//if (Input.GetKeyDown("space") && isTossing == false)
//{
//Throwing();
//}
}
void Shooting()
{
Anim.Play("Pistol_ShootOnce");
GameObject Bullet = Instantiate(Resources.Load("Bullet", typeof(GameObject))) as GameObject;
}
Answer by Pathojen · Jul 21, 2020 at 05:19 PM
Got it! What I had to do was change if(other.gameObject.name =="Bullet")
to if(other.gameObject.name =="Bullet(Clone)")
.
Your answer
Follow this Question
Related Questions
Unity won't Instantiate PLEASE HELP! 1 Answer
My code is instantiating many prefabs, i only want one. 1 Answer
Generating prefabs at origin that are children of moving GameObjects? 2 Answers
How to spawn a prefab at a duplicate objects location 1 Answer
I get an error message every time i run this script? Any ideas? 0 Answers